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

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs