What is the equivalent of PHP's $this->
in Ruby?
相关问题
- Views base64 encoded blob in HTML with PHP
- how to define constructor for Python's new Nam
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Keeping track of variable instances
The ruby equivalent of
this
isself
- they both refer to the current instance.The tricky part is that in Ruby class scope,
self
refers to the current instance of the classClass
that defines the class you are building. Inside a method,self
refers to the instance of the class.eg:
The analog of
$this
isself
, 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
).