What is the Ruby equivalent of the "this" function in Java?

Java has a function called "this" that points to its method. Is there an equivalent in Ruby? For example, is there:

def method this.method end 
+4
source share
4 answers

Equivalent to self . This is also implied. So self.first_name same as first_name inside the class if you are not doing the job.

 class Book attr_reader :first_name, :last_name def full_name # this is the same as self.first_name + ", " + self.last_name first_name + ", " + last_name end end 

When doing the job, you need to explicitly use self , since Ruby does not know whether you assign a local variable named first_name or assign instance.first_name .

 class Book def foo self.first_name = "Bar" end end 
+6
source

There self , for example:

 def account_id self.account.id end 
+1
source

What about

self

Example:

self.name

+1
source

You can call self.whatever the class you are in, is that what you are looking for?

0
source

Source: https://habr.com/ru/post/1491963/


All Articles