Given an array of arguments, how do I send those arguments to a particular function in Ruby?

Posted by Steven Xu on Stack Overflow See other posts from Stack Overflow or by Steven Xu
Published on 2011-01-10T02:40:53Z Indexed on 2011/01/10 2:54 UTC
Read the original article Hit count: 157

Filed under:

Forgive the beginner question, but say I have an array:

a = [1,2,3]

And a function somewhere; let's say it's an instance function:

class Ilike
  def turtles(*args)
    puts args.inspect
  end
end

How do I invoke Ilike.turtles with a as if I were calling (Ilike.new).turtles(1,2,3).

I'm familiar with send, but this doesn't seem to translate an array into an argument list.

A parallel of what I'm looking for is the Javascript apply, which is equivalent to call but converts the array into an argument list.

© Stack Overflow or respective owner

Related posts about ruby