如何在 Ruby 中引用函数?
在 python 中,引用函数相当简单:
>>> def foo():
... print "foo called"
... return 1
...
>>> x = foo
>>> foo()
foo called
1
>>> x()
foo called
1
>>> x
<function foo at 0x1004ba5f0>
>>> foo
<function foo at 0x1004ba5f0>
但是,在 Ruby 中似乎有所不同,因为裸露的 foo
实际上调用了 foo:
ruby-1.9.2-p0 > def foo
ruby-1.9.2-p0 ?> print "foo called"
ruby-1.9.2-p0 ?> 1
ruby-1.9.2-p0 ?> end
=> nil
ruby-1.9.2-p0 > x = foo
foo called => 1
ruby-1.9.2-p0 > foo
foo called => 1
ruby-1.9.2-p0 > x
=> 1
How do I 实际上分配 function foo到 x 然后调用它?或者有更惯用的方法来做到这一点?
In python, it's fairly straightforward to reference a function:
>>> def foo():
... print "foo called"
... return 1
...
>>> x = foo
>>> foo()
foo called
1
>>> x()
foo called
1
>>> x
<function foo at 0x1004ba5f0>
>>> foo
<function foo at 0x1004ba5f0>
However, it seems to be different in Ruby since a naked foo
actually calls foo:
ruby-1.9.2-p0 > def foo
ruby-1.9.2-p0 ?> print "foo called"
ruby-1.9.2-p0 ?> 1
ruby-1.9.2-p0 ?> end
=> nil
ruby-1.9.2-p0 > x = foo
foo called => 1
ruby-1.9.2-p0 > foo
foo called => 1
ruby-1.9.2-p0 > x
=> 1
How do I actually assign the function foo to x and then call it? Or is there a more idiomatic way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Ruby 没有函数。它只有方法(不是一流的)和一流的 Proc,但不与任何对象关联。
所以,这是一个方法:
哦,是的,这是一个真正的方法,而不是顶级函数或过程或其他东西。在顶层定义的方法最终作为
Object
类中的 private(!) 实例方法:这是一个
Proc
:请注意
Proc
s 的调用方式与方法不同:foo.(bar)
语法只是foo.call(bar)
的语法糖(对于Proc
s 和Method
s 也别名为foo[bar]
)。在对象上实现call
方法,然后使用.()
调用它是最接近 Python 的__call__
功能的方法。请注意,Ruby
Proc
和 Python lambda 之间的一个重要区别是没有任何限制:在 Python 中,lambda 只能包含单个语句,但 Ruby 没有 > 语句和表达式之间的区别(一切都是表达式),因此这种限制根本不存在,因此在很多情况下您需要传递一个在 Python 中将命名函数作为参数,因为您无法在单个语句中表达逻辑,而在 Ruby 中您只需传递一个Proc
或一个块即可,这样引用方法的丑陋语法问题就不会出现甚至都不会出现。您可以通过调用
Object#method
将方法包装在Method
对象(本质上是鸭子类型Proc
)中> 对象上的方法(这将为您提供一个Method
,其self
绑定到该特定对象):您还可以使用
Module 中的方法之一#instance_method
系列从模块(或类,显然,因为类是一个模块)获取UnboundMethod
,然后您可以UnboundMethod#bind
到一个特定的对象并调用。 (我认为 Python 具有相同的概念,尽管实现不同:未绑定方法只是显式地接受 self 参数,就像它的声明方式一样。)请注意,您只能将
UnboundMethod
绑定到一个对象,它是您从中获取方法的模块的实例。您不能使用UnboundMethods
在不相关的模块之间“移植”行为:但是请注意,
Method
和UnboundMethod
都是包装器。 围绕方法,而不是方法本身。 Ruby 中的方法不是对象。 (顺便说一句,与我在其他答案中所写的相反。我真的需要回去修复这些问题。)您可以将它们包裹在对象中,但它们不是对象,您可以看到,因为您本质上会遇到与包装器总是相同的问题:身份和状态。如果多次调用同一个方法的method
,每次都会得到不同的Method
对象。如果您尝试在该Method
对象上存储某些状态(例如 Python 风格的__doc__
字符串),则该状态将是该特定私有的< /em> 实例,如果您尝试通过method
再次检索您的文档字符串,您会发现它已经消失了。还有方法引用运算符
.:
形式的语法糖:这与
Ruby doesn't have functions. It only has methods (which aren't first-class) and
Proc
s which are first-class, but are not associated with any object.So, this is a method:
Oh, and, yes, this is a real method, not a top-level function or procedure or something. Methods defined at the top-level end up as private(!) instance methods in the
Object
class:This is a
Proc
:Notice that
Proc
s are called differently from methods:The
foo.(bar)
syntax is just syntactic sugar forfoo.call(bar)
(which forProc
s andMethod
s is also aliased tofoo[bar]
). Implementing acall
method on your object and then calling it with.()
is the closest thing you will get to Python's__call__
ables.Note that an important distinction between Ruby
Proc
s and Python lambdas is that there are no restrictions: in Python, a lambda can only contain a single statement, but Ruby doesn't have the distinction between statements and expressions (everything is an expression), and so this limitation simply doesn't exist, therefore in a lot of cases where you need to pass a named function as an argument in Python because you cannot express the logic in a single statement, you would in Ruby simply pass aProc
or a block instead, so that the problem of the ugly syntax for referencing methods doesn't even arise.You can wrap a method in a
Method
object (which essentially duck-typesProc
) by calling theObject#method
method on an object (which will give you aMethod
whoseself
is bound to that particular object):You can also use one of the methods in the
Module#instance_method
family to get anUnboundMethod
from a module (or class, obviously, since a class is-a module), which you can thenUnboundMethod#bind
to a particular object and call. (I think Python has the same concepts, albeit with a different implementation: an unbound method simply takes the self argument explicitly, just like the way it is declared.)Note that you can only bind an
UnboundMethod
to an object which is an instance of the module you took the method from. You cannot useUnboundMethods
to "transplant" behavior between unrelated modules:Note, however, that both the
Method
and theUnboundMethod
are wrappers around the method, not the method itself. Methods are not objects in Ruby. (Contrary to what I have written in other answers, BTW. I really need to go back and fix those.) You can wrap them in objects, but they aren't objects, and you can see that because you essentially get all the same problems you always get with wrappers: identity and state. If you callmethod
multiple times for the same method, you will get a differentMethod
object every time. If you try to store some state on thatMethod
object (such as Python-style__doc__
strings, for example), that state will be private to that particular instance, and if you try to retrieve your docstring again viamethod
, you will find that it is gone.There is also syntactic sugar in the form of the method reference operator
.:
:Which is identical to
您可以使用从
Object< 继承的
method
实例方法/code>检索
Method
对象,本质上是一个Proc
对象,您可以调用call
。在控制台中,您可以执行以下操作:
You can use the
method
instance method inherited fromObject
to retrieve aMethod
object, which essentially is aProc
object which you can invokecall
on.In the console, you'd do this:
Ruby 支持
proc
和lambda
在其他语言中可能称为匿名函数或闭包,具体取决于关于如何使用它们。他们可能更接近您正在寻找的东西。Ruby supports
proc
andlambda
which in other languages might be called anonymous functions or closures, depending on how they are used. They might be closer to what you are looking for.从 https://stackoverflow.com/a/26620095/226255 复制的函数和方法之间的(主要)区别
Ruby 没有函数,您的
def foo
最终成为Object
类的方法。如果您坚持像上面那样定义
foo
,您可以通过执行以下操作提取其“功能”:说明:
重要说明:
to_proc
“复制”方法对象的关联实例变量(如果有)。考虑一下:从概念上讲,如果您想要一个可以在某种类型的对象上工作的“函数”,它应该是一个方法,并且您应该这样组织您的代码。如果您只需要在特定上下文中使用“函数”并希望传递它,请使用 lambda:
The (main) difference between functions and methods as copied from https://stackoverflow.com/a/26620095/226255
Ruby does not have functions and your
def foo
ends up being a method for theObject
class.If you insist on defining
foo
like you're doing above, you can extract its "functionality" by doing this:Explanation:
Important note:
to_proc
"copies" the method's object's associated instance variables if any. Consider this:Conceptually, if you want a "function" that would work on a certain type of objects, it should be a method and you should organize your code as such. If you only need your "function" in a certain context and wish to pass it around, use lambdas: