I am trying to call a method that has multiple arguments using reflection. However, when I invoke the method.call method passing the arguments as an array it chokes with the following error:
ArgumentError (wrong number of arguments (1 for 4))
The ruby interpreter thinks that my single array is the parameter I am passing in; however, the array I am passing in contains 4 object that map directly to the 4 arguments the interpreter is looking for. Is there another way to accomplish this other than using method.call? Or am I using method.call incorrectly?
What is making this more difficult is the fact that the call I am making is completely dynamic so just calling:
method_object.call arg1, arg2, arg3, arg4
is not an option.
thanks for any tips,
~harris
Harris Reynolds wrote:
I am trying to call a method that has multiple arguments using reflection. However, when I invoke the method.call method passing the arguments as an array it chokes with the following error:
ArgumentError (wrong number of arguments (1 for 4))
The ruby interpreter thinks that my single array is the parameter I am passing in; however, the array I am passing in contains 4 object that map directly to the 4 arguments the interpreter is looking for. Is there another way to accomplish this other than using method.call? Or am I using method.call incorrectly?
What is making this more difficult is the fact that the call I am making is completely dynamic so just calling:
method_object.call arg1, arg2, arg3, arg4
is not an option.
thanks for any tips,
Try:
method_object.call(*args)
with args being the name of your argument array.
Jamey
Put a * before the array argument, it'll make Ruby expand it to fit
the arguments, eg:
def test(one, two, three, four)
puts "#{one} #{two} #{three} #{four}"
end
args = [1, 2, 3, 4]
m = method(:test)
m.call(*args)
···
On 8/2/06, Harris Reynolds <hreynolds2@yahoo.com> wrote:
The ruby interpreter thinks that my single array is the parameter I am passing in; however, the array I am passing in contains 4 object that map directly to the 4 arguments the interpreter is looking for. Is there another way to accomplish this other than using method.call? Or am I using method.call incorrectly?
--
Phillip Hutchings
http://www.sitharus.com/
method.call(*[arg1, arg2, arg3, arg4])
···
On Aug 1, 2006, at 4:04 PM, Harris Reynolds wrote:
I am trying to call a method that has multiple arguments using reflection. However, when I invoke the method.call method passing the arguments as an array it chokes with the following error:
ArgumentError (wrong number of arguments (1 for 4))
The ruby interpreter thinks that my single array is the parameter I am passing in; however, the array I am passing in contains 4 object that map directly to the 4 arguments the interpreter is looking for. Is there another way to accomplish this other than using method.call?
--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant
http://trackmap.robotcoop.com
Phillip Hutchings wrote:
The ruby interpreter thinks that my single array is the parameter I am passing in; however, the array I am passing in contains 4 object that map directly to the 4 arguments the interpreter is looking for. Is there another way to accomplish this other than using method.call? Or am I using method.call incorrectly?
Put a * before the array argument, it'll make Ruby expand it to fit
the arguments, eg:
def test(one, two, three, four)
puts "#{one} #{two} #{three} #{four}"
end
args = [1, 2, 3, 4]
m = method(:test)
m.call(*args)
Note that you do not need to use Method#call to do this, it works for all methods (since the OP was asking if he had to use it).
The above could have been (just for clarification):
def test(one, two, three, four)
puts "#{one} #{two} #{three} #{four}"
end
test(*[1, 2, 3, 4])
or
args = [1,2,3,4]
test(*args)
-Justin
···
On 8/2/06, Harris Reynolds <hreynolds2@yahoo.com> wrote:
Justin Collins wrote:
Phillip Hutchings wrote:
The ruby interpreter thinks that my single array is the parameter I am passing in; however, the array I am passing in contains 4 object that map directly to the 4 arguments the interpreter is looking for. Is there another way to accomplish this other than using method.call? Or am I using method.call incorrectly?
Put a * before the array argument, it'll make Ruby expand it to fit
the arguments, eg:
def test(one, two, three, four)
puts "#{one} #{two} #{three} #{four}"
end
args = [1, 2, 3, 4]
m = method(:test)
m.call(*args)
Note that you do not need to use Method#call to do this, it works for all methods (since the OP was asking if he had to use it).
Or maybe he wasn't. Either way.
-Justin
···
On 8/2/06, Harris Reynolds <hreynolds2@yahoo.com> wrote: