在Ruby中实现“调用用户函数数组”
我如何在ruby中完成任务 所以我可以这样做:在Ruby中实现“调用用户函数数组”,ruby,Ruby,我如何在ruby中完成任务 所以我可以这样做: class App def foo(a,b) puts a + b end def bar args = [1,2] App.send(:foo, args) # doesn't work App.send(:foo, args[0], args[1]) # does work, but does not scale end end 尝试爆炸阵列 Ap
class App
def foo(a,b)
puts a + b
end
def bar
args = [1,2]
App.send(:foo, args) # doesn't work
App.send(:foo, args[0], args[1]) # does work, but does not scale
end
end
尝试爆炸阵列
App.send(:foo, *args)
删除了
php
标记,因为这不是一个php问题。为了完整起见:“分解”数组意味着解释器将把args中的所有项作为单独的参数发送。在Ruby习惯用法中,一元*
被称为splat操作符。它不仅仅适用于数组。如果它还不是数组,则将使用到_ary
和到_a
将其转换为一个数组。