Getting ruby function object itself

2020-02-28 18:06发布

In Ruby, everything is supposed to be an object. But I have a big problem to get to the function object defined the usual way, like

def f
    "foo"
end

Unlike in Python, f is the function result, not the function itself. Therefore, f(), f, ObjectSpace.f are all "foo". Also f.methods returns just string method list.

How do I access the function object itself?

7条回答
干净又极端
2楼-- · 2020-02-28 18:52

That isn't a function; it's a method of an object. If you want to get an object representing a method, you ask the owning object for it:

someobj.method :f

For example:

plus_one = 1.method :+
plus_one[9] # 10
查看更多
登录 后发表回答