class Y def x(y) p y end end class Yx < Y def initialize(x) self.x(x) end end a = Yx.new(11)
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.
欢迎光临 Project1 (https://rpg.blue/) | Powered by Discuz! X3.1 |