[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Passing the name of an argument and then its value

anne001

7/19/2006 9:48:00 PM

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

7 Answers

mathew

7/19/2006 10:15:00 PM

0

anne001 wrote:
> 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?

A common idiom is to make the method take a hash of arguments:

def somemethod(hashargs)
# Set up defaults
port = 80
protocol = 'http'
flavor = 'vanilla'
retries = 0
# Override from arguments
port = hashargs[:port] if hashargs.key?(:port)
protocol = hashargs[:protocol] if hashargs.key?(:protocol)
flavor = hashargs[:flavor] if hashargs.key?(:flavor)
retries = hashargs[:retries] if hashargs.key?(:retries)
# Report result
puts "#{protocol}://example.com:#{port}/#{flavor} + #{retries} retries"
end

somemethod(:protocol => 'ftp', :port => 8000,
:flavor => 'pineapple', :retries => 4)

somemethod(:flavor => 'chocolate')


mathew
--
<URL:http://www.pobox.com/...
My parents went to the lost kingdom of Hyrule
and all I got was this lousy triforce.

Logan Capaldo

7/19/2006 10:21:00 PM

0


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

> 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
>
>

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"




anne001

7/19/2006 10:51:00 PM

0

Sorry, I was not clear enough, the soap4r fx is generated by a ruby fx
wsdl2ruby.rb, it is a given.
the order of the arguments is derived from an Amazon wsdl file, it is
also given.

So given a fx with 30+ arguments, most of them empty, how can I access
it with
just the arguments which are not empty with the argument name.

anne001

7/19/2006 11:37:00 PM

0

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

anne001

7/20/2006 12:30:00 AM

0

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

anne001

7/20/2006 1:09:00 AM

0

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

mathew

7/20/2006 10:40:00 AM

0

anne001 wrote:
> Sorry, I was not clear enough, the soap4r fx is generated by a ruby fx
> wsdl2ruby.rb, it is a given.
> the order of the arguments is derived from an Amazon wsdl file, it is
> also given.
>
> So given a fx with 30+ arguments, most of them empty, how can I access
> it with just the arguments which are not empty with the argument name.

Create a wrapper or facade method, using the idiom I posted.


mathew
--
<URL:http://www.pobox.com/...
My parents went to the lost kingdom of Hyrule
and all I got was this lousy triforce.