在Ruby中实现`call_user_func_array`

bal*_*ton 5 ruby

我怎样才能在ruby中完成http://php.net/manual/en/function.call-user-func-array.php

所以我可以这样做:

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
Run Code Online (Sandbox Code Playgroud)

joh*_*man 8

尝试爆炸阵列

App.send(:foo, *args)
Run Code Online (Sandbox Code Playgroud)

  • 只是为了完整性:'Exploding'数组意味着解释器将args中的所有项目作为单独的参数发送.一元`*`在Ruby习语中被称为__splat__运算符. (3认同)