# Passing the name of an argument and then its value

**URL:** <https://www.ruby-forum.com/t/passing-the-name-of-an-argument-and-then-its-value/99039>\
**Category:** Ruby\
**Created:** [July 19, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/passing-the-name-of-an-argument-and-then-its-value/99039 "2006-07-19T23:52:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [July 19, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/passing-the-name-of-an-argument-and-then-its-value/99039/1 "2006-07-19T23:52:08Z")

</div>

I have a soap4r created fx to search amazon which has too many  
arguments to use comfortably  
itemSearchRequest = ItemSearchRequest.new("", “”, “”, “”, “”, “”, “”,  
“”, “”, “”, “”, “”,  
“”,"","","","","",“gabin”,"","","","","","","","","","","",“Books”,"","","","","")?

I would like to only type the name of the key search and the value. I  
came up with some code which almost works. Can I do simpler, easier to  
read than this? or more principled, more elegant?  
------------------\>  
#Here is a simplified version of the fx, with just 2 elements  
class ItemSearchRequest  
attr\_accessor :actor  
attr\_accessor :artist  
def initialize(actor = nil, artist = nil)  
@actor = actor  
@artist = artist  
end  
end  
itemSearchRequest = ItemSearchRequest.new  
p itemSearchRequest.inspect  
itemSearchRequest = ItemSearchRequest.new("", “dud”)  
p itemSearchRequest.inspect  
itemSearchRequest = ItemSearchRequest.new(artist=“dod”)  
p itemSearchRequest.inspect

--------\>

# this is the method I wrote to simplify the accessing of the fx

def namedrequest(fxname,hash)  
nameinfo=eval(fxname).new.inspect  
p nameinfo

# should return something like the following

# nameinfo="#\<ItemSearchRequest:0x25cd4 @artist=nil, @actor=nil\>"

command=fxname.slice(0,1).downcase+fxname.slice(1,fxname.length)+"="+fxname+".new("  
namearray=nameinfo.scan(/@(\w+)=/)  
namearray.each{ |key|  
p key  
p key[0]  
command = command +’"’+ (hash[key[0]] || “”)+’",’  
}  
#replace last coma with )  
command = command.sub(/,$/,")")  
p command  
eval(command)  
end

namedrequest(“ItemSearchRequest”,{“artist” =\>“dod”, “actor” =\>“dud”})  
p itemSearchRequest

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [July 20, 2006, 12:24am UTC](https://www.ruby-forum.com/t/passing-the-name-of-an-argument-and-then-its-value/99039/2 "2006-07-20T00:24:01Z")

</div>

On Jul 19, 2006, at 5:50 PM, anne001 wrote:

> ------------------\>  
> p itemSearchRequest.inspect
> 
> # should return something like the following
> 
> ```
> }
> 
> ```
> 
> #replace last coma with )  
> command = command.sub(/,$/,")")  
> p command  
> eval(command)  
> end
> 
> namedrequest(“ItemSearchRequest”,{“artist” =\>“dod”, “actor” =\>“dud”})  
> p itemSearchRequest

I would use a hash, a splat and an array. My solution follows, it  
avoids the use of eval.

% cat lots\_of\_params.rb

def wrapper(hash)  
parameter\_names\_in\_order\_passed\_to\_func = %w[param\_first  
param\_second param\_third]

array\_of\_arguments = Array.new  
(parameter\_names\_in\_order\_passed\_to\_func.length, ‘’)  
hash.keys.each do |key|  
array\_of\_arguments[parameter\_names\_in\_order\_passed\_to\_func.index  
(key)] = hash[key]  
end  
real\_func(\*array\_of\_arguments)  
end

def real\_func(param\_first, param\_second, param\_third)  
puts “param\_first=#{param\_first.inspect}”  
puts “param\_second=#{param\_second.inspect}”  
puts “param\_third=#{param\_third.inspect}”  
end

wrapper(‘param\_second’ =\> ‘hello’)  
puts  
wrapper(‘param\_third’ =\> ‘ok’)  
puts  
wrapper(‘param\_first’ =\> ‘hello’, ‘param\_third’ =\> ‘blah’)

% ruby lots\_of\_params.rb  
param\_first=""  
param\_second=“hello”  
param\_third=""

param\_first=""  
param\_second=""  
param\_third=“ok”

param\_first=“hello”  
param\_second=""  
param\_third=“blah”

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [July 20, 2006, 1:42am UTC](https://www.ruby-forum.com/t/passing-the-name-of-an-argument-and-then-its-value/99039/3 "2006-07-20T01:42:24Z")

</div>

I don’t really want to write the names out! and I want it to be general  
to apply to other methods in the wsdl amazon series.

but I like your idea, and it almost works, much cleaner way of passing  
the parameters.  
array\_of\_names.index(key) is nil in my case… I have to figure out  
why. but I can use my own code for this if I don’t find it. Anyway,  
thank you so much for your time and ideas.

def namedrequest(fxname,hash)  
nameinfo=eval(fxname).new.inspect

array\_of\_names=nameinfo.scan(/@(\w+)=/)  
array\_of\_arguments=Array.new(array\_of\_names.length,’’)  
hash.keys.each{ |key|  
array\_of\_arguments[array\_of\_names.index(key)]=hash[key]  
}

command=fxname.slice(0,1).downcase+fxname.slice(1,fxname.length)+"="+fxname+".new(\*array\_of\_arguments)"  
p command  
eval(command)  
end

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [July 20, 2006, 2:32am UTC](https://www.ruby-forum.com/t/passing-the-name-of-an-argument-and-then-its-value/99039/4 "2006-07-20T02:32:17Z")

</div>

I don’t really want to write the names out! and I want it to be general  
to apply to other methods in the wsdl amazon series.

but I like your idea, and it almost works, much cleaner way of passing  
the parameters.  
array\_of\_names.index(key) is nil in my case… I have to figure out  
why. but I can use my own code for this if I don’t find it. Anyway,  
thank you so much for your time and ideas.

def namedrequest(fxname,hash)  
nameinfo=eval(fxname).new.inspect

array\_of\_names=nameinfo.scan(/@(\w+)=/)  
array\_of\_arguments=Array.new(array\_of\_names.length,’’)  
hash.keys.each{ |key|  
array\_of\_arguments[array\_of\_names.index(key)]=hash[key]  
}

command=fxname.slice(0,1).downcase+fxname.slice(1,fxname.length)+"="+fxname+".new(\*array\_of\_arguments)"  
p command  
eval(command)  
end

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [July 20, 2006, 3:12am UTC](https://www.ruby-forum.com/t/passing-the-name-of-an-argument-and-then-its-value/99039/5 "2006-07-20T03:12:17Z")

</div>

oups, sorry about the repeat

array\_of\_names=array\_of\_names.flatten fixes some of the problems

but the real problem is the eval:  
if array\_of\_arguments is  
[“dod”, “dud”]  
and command is  
“itemSearchRequest=ItemSearchRequest.new(\*array\_of\_arguments)”  
the result is  
#\<ItemSearchRequest:0x2534c @artist=nil, @actor=“dod”\>

I am not sure why
