# Sending Multiple Args to a Method Object

**URL:** <https://www.ruby-forum.com/t/sending-multiple-args-to-a-method-object/100942>\
**Category:** Ruby\
**Created:** [August 2, 2006, 1:05am UTC](https://www.ruby-forum.com/t/sending-multiple-args-to-a-method-object/100942 "2006-08-02T01:05:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harris\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@Harris\_R](https://www.ruby-forum.com/u/Harris_R)\
**Post date:** [August 2, 2006, 1:05am UTC](https://www.ruby-forum.com/t/sending-multiple-args-to-a-method-object/100942/1 "2006-08-02T01:05:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Harris\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@Harris\_R](https://www.ruby-forum.com/u/Harris_R)\
**Post date:** [August 2, 2006, 1:15am UTC](https://www.ruby-forum.com/t/sending-multiple-args-to-a-method-object/100942/2 "2006-08-02T01:15:11Z")

</div>

Harris R. wrote:

> is not an option.
> 
> thanks for any tips,

Try:

method\_object.call(\*args)

with args being the name of your argument array.

Jamey

---

<div class="post-metadata">

**Author:** ![Harris\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@Harris\_R](https://www.ruby-forum.com/u/Harris_R)\
**Post date:** [August 2, 2006, 1:32am UTC](https://www.ruby-forum.com/t/sending-multiple-args-to-a-method-object/100942/3 "2006-08-02T01:32:11Z")

</div>

On 8/2/06, Harris R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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)

---

<div class="post-metadata">

**Author:** ![Harris\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@Harris\_R](https://www.ruby-forum.com/u/Harris_R)\
**Post date:** [August 2, 2006, 1:38am UTC](https://www.ruby-forum.com/t/sending-multiple-args-to-a-method-object/100942/4 "2006-08-02T01:38:22Z")

</div>

Phillip H. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Harris\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@Harris\_R](https://www.ruby-forum.com/u/Harris_R)\
**Post date:** [August 2, 2006, 1:41am UTC](https://www.ruby-forum.com/t/sending-multiple-args-to-a-method-object/100942/5 "2006-08-02T01:41:44Z")

</div>

On Aug 1, 2006, at 4:04 PM, Harris R. wrote:

> using method.call?  
> method.call(\*[arg1, arg2, arg3, arg4])

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Harris\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/53a042/32.png) [@Harris\_R](https://www.ruby-forum.com/u/Harris_R)\
**Post date:** [August 2, 2006, 1:51am UTC](https://www.ruby-forum.com/t/sending-multiple-args-to-a-method-object/100942/6 "2006-08-02T01:51:26Z")

</div>

Justin C. wrote:

> >

Or maybe he wasn’t. Either way.

-Justin
