Ruby 2 Self

At every point when your program is running, there is one and only one self - the current or default object accessible to you in your program. You can tell which object self represents by following a small set of rules.

Top level context

The top level context is before you have entered any other context, such as a class definition. Therefore the term top level refers to program code written outside of a class or module. If you open a new text file and type:
  1. x = 1  
you have created a top level local variable x. If you type:
  1. def m  
  2. end  
you have created a top level method - an instance method of Object (even though self is not Object). Top-level methods are always private. Ruby provides you with a start-up self at the top level. If you type:
  1. puts self  
it displays main - a special term the default self object uses to refer to itself. The class of the main object is Object.

Self inside class and module definitions

In a class or module definition, self is the class or module object.
  1. # p063xself1.rb  
  2. class S  
  3.   puts 'Just started class S'  
  4.   puts self  
  5.   module M  
  6.     puts 'Nested module S::M'  
  7.     puts self  
  8.   end  
  9.   puts 'Back in the outer level of S'  
  10.   puts self  
  11. end  
The output is:
  1. >ruby p063xself1.rb  
  2. Just started class S  
  3. S  
  4. Nested module S::M  
  5. S::M  
  6. Back in the outer level of S  
  7. S  
  8. >Exit code: 0  

Self in instance method definitions

At the time the method definition is executed, the most you can say is that self inside this method will be some future object that has access to this method.
  1. # p063xself2.rb  
  2. class S  
  3.   def m  
  4.     puts 'Class S method m:'  
  5.     puts self  
  6.   end  
  7. end  
  8. s = S.new  
  9. s.m  
The output is:
  1. >ruby p063xself2.rb  
  2. Class S method m:  
  3. #<S:0x2835908>  
  4. >Exit code: 0  
The output #<S:0x2835908> is Ruby's way of saying "an instance of S".

Self in singleton-method and class-method definitions

Singleton methods - those attached to a particular object can be called by only one object. When a singleton method is executed, self is the object that owns the method, as shown below:
  1. # p063xself3.rb  
  2. obj = Object.new  
  3. def obj.show  
  4.   print 'I am an object: '  
  5.   puts "here's self inside a singleton method of mine:"  
  6.   puts self  
  7. end  
  8. obj.show  
  9. print 'And inspecting obj from outside, ' 
  10. puts "to be sure it's the same object:"  
  11. puts obj  
The output of the above example is:
  1. >ruby p063xself3.rb  
  2. I am an object: here's self inside a singleton method of mine: 
  3. #<Object:0x2835688> 
  4. And inspecting obj from outside, to be sure it's the same object:  
  5. #<Object:0x2835688>  
  6. >Exit code: 0  
Class methods are defined as singleton methods for class objects. Refer to the following program:
  1. # p063xself4.rb  
  2. class S  
  3.   def S.x  
  4.     puts "Class method of class S"  
  5.     puts self  
  6.   end  
  7. end  
  8. S.x  
The output is:
  1. >ruby p063xself4.rb  
  2. Class method of class S  
  3. S  
  4. >Exit code: 0  
self inside a singleton method (a class method, in this case) is the object whose singleton method it is.