在脚本中看到了这样的句子,但是不知道究竟是什么,它有什么用
找到了特殊变量的说明,self:当前方法的执行对象本身。
这句话是什么意思啊
【苦思了一阵之后】
学过c的娃太苦了,对象总是不好理解,每一次以为理解了,但实际上对象还是很晦涩而深奥啊
[pre lang="ruby" line="1"]class Y
def x(y)
p y
end
end
class Yx < Y
def initialize(x)
self.x(x)
end
end
a = Yx.new(11)[/pre]
看我这串代码,这个a就是当前方法的执行对象吗?
The keyword self in Ruby gives you access to the current object – the object that is receiving the current message. To explain: a method call in Ruby is actually the sending of a message to a receiver. When you write obj.meth, you're sending the meth message to the object obj. obj will respond to meth if there is a method body defined for it. And inside that method body, self refers to obj.
我的言论只代表我个人的观点,不代表雇主及/或任何第三方的立场。
Opinions expressed are solely my own and do not express the views or opinions of my employer and/or any third parties. 捐赠 | GitHub