Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Variable number of arguments for methods called with "send" (http://www.velocityreviews.com/forums/t862655-variable-number-of-arguments-for-methods-called-with-send.html)

Pierre Lecocq 04-27-2010 01:38 PM

Variable number of arguments for methods called with "send"
 
Hi there,


I have a (soooo) dummy class :


# --

class Main < XXXXX

def time
'Il est ' + Time.now.to_s
end

def salut
['salut', 'les', 'musclés']
end

def test_with_args(arg1, arg2)
[arg1, arg2]
end

def test_with_multiple_args(*args)
args
end

end

# /--




In a configuration file, I have the name of the methods and their
arguments if they need some:



# --

config:
aff_bonjour: __SALUT
aff_temps: __TIME
tab_toto: __TEST_WITH_ARGS 'toto', 53

# /--




In another script, I get back the config file and try to call the
lethods in order to stack their results.
In order to do that, I wrote something like:


# --

vars = {}
config = YAML::load('config.yml') # the config file described previously
@name = 'Main' # got from config file
obj = Object.const_get(@name).new # Create the object
config.each do |key, value|
if value =~ /^__(.+)/ and !obj.nil?
tmp = value.gsub(/^__/, '').downcase.split(' ')
method = tmp[0] # get the method name
tmp.delete_at 0 # delete method name from the array
tmp.map do |e|
e.gsub!(/,/, '') # clean up remaining args
e.strip!
end
vars[key.to_s] = obj.send(method.to_sym) if obj.respond_to? method
else
vars[key.to_s] = value
end
end

# /--



Here is the small description

1. After preparing the object, I check every line of my config
2. if it is a method (begins by '__'), I get the optional args

e.g: "tab_toto: __TEST_WITH_ARGS 'toto', 53"
"test_with_args" is the method
'toto' and 53 are the args

3. I call the method by invoking "obj.send(method.to_sym)"
4 (back to 2) and if it is not a method, I just copy the static value
to my "vars" hash




And here is my problem :

for a method that does not take any argument (like "time" or "salut"),
there is no problem, it works like a charm.

But if I want to call a method with arguments, I am stuck because I do
not know how many arguments it needs and what is the "kind" of arguments
(can be "static arguments" - arg1, arg2 - or VARGS - *args - )

How can I call a method with a dynamic array (even empty) to cover all
the different kind of methods of my "Main" class ?

Or is there any other solution for that ?




I do not know if those explanations are clear enough, tell me if not.

Thank you for your potential help,
Regards,

Pierre
--
Posted via http://www.ruby-forum.com/.


Brian Candler 04-27-2010 02:18 PM

Re: Variable number of arguments for methods called with "send"
 
Pierre Lecocq wrote:
> But if I want to call a method with arguments, I am stuck because I do
> not know how many arguments it needs and what is the "kind" of arguments
> (can be "static arguments" - arg1, arg2 - or VARGS - *args - )


There is no difference between those cases.

args = []
send(:foo, *args)
# same as: send(:foo)

args = ["abc"]
send(:foo, *args)
# same as: send(:foo, "abc")

args = ["abc","def"]
send(:foo, *args)
# same as: send(:foo, "abc", "def")

HTH,

Brian.
--
Posted via http://www.ruby-forum.com/.


Pierre Lecocq 04-27-2010 02:23 PM

Re: Variable number of arguments for methods called with "send"
 
Thanks a lot Brian.
In fact, I didn't put the star before my args array.
That is why it crashed when the method does not take arguments.

Thanks again !


Brian Candler wrote:
> Pierre Lecocq wrote:
>> But if I want to call a method with arguments, I am stuck because I do
>> not know how many arguments it needs and what is the "kind" of arguments
>> (can be "static arguments" - arg1, arg2 - or VARGS - *args - )

>
> There is no difference between those cases.
>
> args = []
> send(:foo, *args)
> # same as: send(:foo)
>
> args = ["abc"]
> send(:foo, *args)
> # same as: send(:foo, "abc")
>
> args = ["abc","def"]
> send(:foo, *args)
> # same as: send(:foo, "abc", "def")
>
> HTH,
>
> Brian.


--
Posted via http://www.ruby-forum.com/.



All times are GMT. The time now is 03:29 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57