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

后端 未结 1 1355
误落风尘
误落风尘 2020-12-04 17:43

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:

<
相关标签:
1条回答
  • 2020-12-04 18:05

    As you know, when you define a method, you can use the * to turn a list of arguments into an array. Similarly when you call a method you can use the * to turn an array into a list of arguments. So in your example you can just do:

    Ilike.new.turtles(*a)
    
    0 讨论(0)
提交回复
热议问题