# Variable number of arguments for methods called with "send"

**URL:** <https://www.ruby-forum.com/t/variable-number-of-arguments-for-methods-called-with-send/187665>\
**Category:** Ruby\
**Created:** [April 27, 2010, 3:38pm UTC](https://www.ruby-forum.com/t/variable-number-of-arguments-for-methods-called-with-send/187665 "2010-04-27T15:38:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierrebombay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pierrebombay](https://www.ruby-forum.com/u/pierrebombay)\
**Post date:** [April 27, 2010, 3:38pm UTC](https://www.ruby-forum.com/t/variable-number-of-arguments-for-methods-called-with-send/187665/1 "2010-04-27T15:38:11Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![pierrebombay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pierrebombay](https://www.ruby-forum.com/u/pierrebombay)\
**Post date:** [April 27, 2010, 4:18pm UTC](https://www.ruby-forum.com/t/variable-number-of-arguments-for-methods-called-with-send/187665/2 "2010-04-27T16:18:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pierrebombay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pierrebombay](https://www.ruby-forum.com/u/pierrebombay)\
**Post date:** [April 27, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/variable-number-of-arguments-for-methods-called-with-send/187665/3 "2010-04-27T16:22:37Z")

</div>

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