Ruby equivalent to PHP's $this

前端 未结 2 1991
野的像风
野的像风 2020-12-31 20:15

What is the equivalent of PHP\'s $this-> in Ruby?

2条回答
  •  -上瘾入骨i
    2020-12-31 20:37

    The analog of $this is self, as has been mentioned. However, you asked about $this->, which means you want to use it to access an instance variable ($this->somevar) or instance method (this->somemethod()). For an instance variable, the equivalent in Ruby would be @ (as in @somevar). For instance methods, the equivalent would be to just write the method name (somemethod), or, if you like to be verbose (self.somemethod).

提交回复
热议问题