[Ruby] What is self

less than 1 minute read

What is #self The object itself. Inside the method, the instance to which the method belongs can be referenced by a pseudo variable named self. This in other languages. Characteristics of #self When a method call that omits the receiver is made in the method, self becomes the receiver. When the receiver can be omitted, it is often omitted.

How to use

class Numbers
   attr_accessor :number
   def set_number
     self.number = 30 # put 30 when calling this class number
   end
end

num1 = Numbers.new
num1.set_number
num1.number # => 30

Tags:

Updated: