How do I reference a function in Ruby?

前端 未结 4 1596
旧巷少年郎
旧巷少年郎 2020-12-05 00:32

In python, it\'s fairly straightforward to reference a function:

>>> def foo():
...     print \"foo called\"
...     return 1
... 
>>> x =          


        
4条回答
  •  被撕碎了的回忆
    2020-12-05 00:51

    You can use the method instance method inherited from Object to retrieve a Method object, which essentially is a Proc object which you can invoke call on.

    In the console, you'd do this:

    fooMethod = self.method(:foo) #fooMethod is a Method object
    
    fooMethod.call #invokes fooMethod
    

    alt text

提交回复
热议问题