Forum: Ruby Extracting array values in Object.send.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5e8fd591aeccbffd6fc93c171ac4fa43?d=identicon&s=25 Aaron Smith (warhero)
on 2007-01-13 22:05
How can I exract array values when using Object.send. See the example
below.

class TestSend

  def test( arg1, arg2, arg3)
    puts arg1
    puts arg2
    puts arg3
  end

end

t = TestSend.new
t.send('test', 'hey', 1, 'rrr')

args = ['hey',1,'rrr']
t.send('test', args) #this throws an argument error, 3 for 1..

Thanks.
Bc368ef524130e8d0deb386de961e24a?d=identicon&s=25 Suraj Kurapati (snk)
on 2007-01-13 22:37
Aaron Smith wrote:
> How can I exract array values when using Object.send. See the example
> below.
>
> class TestSend
>
>   def test( arg1, arg2, arg3)
>     puts arg1
>     puts arg2
>     puts arg3
>   end
>
> end
>
> t = TestSend.new
> t.send('test', 'hey', 1, 'rrr')
>
> args = ['hey',1,'rrr']
> t.send('test', args) #this throws an argument error, 3 for 1..

Use the splat operator to expand the array into individual method
arguments:

  t.send(:test, *args)
This topic is locked and can not be replied to.