Ruby相当于PHP的$ this [英] Ruby equivalent to PHP's $this
本文介绍了Ruby相当于PHP的$ this的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Ruby中,PHP的$this->
等效于什么?
What is the equivalent of PHP's $this->
in Ruby?
推荐答案
this
的红宝石等效值为self
-它们都引用当前实例.
The ruby equivalent of this
is self
- they both refer to the current instance.
棘手的部分是,在Ruby类作用域中,self
引用了类Class
的当前实例,该实例定义了您要构建的类.在方法内部,self
引用该类的实例.
The tricky part is that in Ruby class scope, self
refers to the current instance of the class Class
that defines the class you are building. Inside a method, self
refers to the instance of the class.
例如:
class Example
puts self # => "Example" - the stringified class object
def foo
puts self # #<Example:0xdeadbeef> - the stringified instance
end
end
这篇关于Ruby相当于PHP的$ this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文