在 Ruby 中,“接收者"是做什么的?参考? [英] In Ruby what does the "receiver" refer to?
本文介绍了在 Ruby 中,“接收者"是做什么的?参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在阅读一篇关于具有接收器的方法的文档.什么是接收器?
I'm reading a document that talks about a method having a receiver. What's a receiver?
推荐答案
在 Ruby(以及其他受 SmallTalk 启发的语言)中,对象被视为发送和接收消息".
In Ruby (and other languages that take inspiration from SmallTalk) objects are thought of as sending and receiving 'messages'.
在 Ruby 中,Object,万物的基类,有一个发送方法:Object.send 例如:
In Ruby, Object, the base class of everything, has a send method: Object.send For example:
class Klass
def hello
"Hello!"
end
end
k = Klass.new
k.send :hello #=> "Hello!"
k.hello #=> "Hello!"
在这两种情况下,k 都是hello"消息的接收者.
In both of these cases k is the receiver of the 'hello' message.
这篇关于在 Ruby 中,“接收者"是做什么的?参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文