Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Variable number of arguments for methods called with "send"

Reply
Thread Tools

Variable number of arguments for methods called with "send"

 
 
Pierre Lecocq
Guest
Posts: n/a
 
      04-27-2010
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/.

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      04-27-2010
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/.

 
Reply With Quote
 
 
 
 
Pierre Lecocq
Guest
Posts: n/a
 
      04-27-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a function that expects variable arguments from a functionwith variable arguments Navaneeth C Programming 4 11-20-2010 05:35 AM
VA_START called inside a function with fixed number of arguments (#2) tukki C++ 0 07-30-2010 11:22 AM
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
VA_START called inside a function with fixed number of arguments mahesha C++ 3 10-05-2006 10:42 PM
Private methods called from public methods Robert Javascript 16 05-06-2006 11:32 PM



Advertisments