codedecoder

breaking into the unknown…

self receiver and ancestor in ruby

Leave a comment

1 => receiver :

In simple word receiver are object on which you call a method. syntactically, the object before the . operator are receiver

Example :

@user.full_name

self.full_name

here we are calling the method full_name on @user and self, so @user and self are receiver here. They are called explicit receiver as we have specifically called the method on them. But when we call any method without . operator , ruby decide the receiver itself using self and that is called implicit receiver .

2 => ancestor chain :

When ever a method is called, it try to find the method in the receiver(current object). If it is not find there, it try to find it in its parent, if again not found there, it move up to the next parent and keep doing till the last parent(this is called method lookup). Thus the class hierarchy from bottom to up where a method is searched is called ancestor chain.

NOTE : a object contain its own variable but not its own methods. It just contain reference to methods

3 => self :

As discussed above, whenever we call a method, ruby do method lookup in the ancestor chain. Now when method is found, it try to execute it on current object. The ruby decide the current object from self irrespective of whether method is called on implicit or explicit receiver. See what happen when we call method full name with explicit receiver @user

@user.full_name

  1. switch self to receiver (@user)
  2. look up method (full_name) in self’s class (objects do not store methods, only classes can)
  3. invoke method (full_name)

So we can conclude below about self

  • In Ruby, self is a special variable(not a object) that always references the current object.
  • self (current object) is the default receiver of method calls. This means that if We call a method and don’t give an explicit receiver (We don’t put something before the dot) then Ruby will always look into the object referenced by self for that method.
  • self (current object) is where instance variables are found. This means that if We write @name then it’s going to go and look into the object referenced by self for that particular instance variable. It’s to be noted that instance variables are not defined by a class, they are unrelated to sub-classing and the inheritance mechanism.
  • At any point of a running program there will be one and only one self i,e current object referenced by it

Let us see, what self represent at any point of a running program. create a self_demo.rb file as below

puts "self at global level i,e above class defination => #{self}"
class A
  puts "self at just after entering class A => #{self}"
  module M
    puts "self at just after entering module M => #{self}"
    module N
      puts "self at just after entering module N => #{self}"
      class B
        puts "self at just after entering class B in module N => #{self}"
        def method_b
          puts "self in method_b of class B => #{self}"
        end
      end
      puts "self just after existing class B => #{self}"
    end
    puts "self just after existing module M => #{self}"
  end
  puts "self just befor end of class A => #{self}"
end

puts "Calling the method_b"
@obj = A::M::N::B.new
@obj.method_b

puts "demo of self in singleton method"

def @obj.singleton_m
puts "self inside a singleton method of object of class B => #{self}"
end

@obj.singleton_m

run the above file on console with ruby to see the output of above program

$ ruby self_demo.rb
self at global level i,e above class defination => main
self at just after entering class A => A
self at just after entering module M => A::M
self at just after entering module N => A::M::N
self at just after entering class B in module N => A::M::N::B
self just after existing class B => A::M::N
self just after existing module M => A::M
self just befor end of class A => A
Calling the method_b
self in method_b of class B => #<A::M::N::B:0x819cab4>
demo of self in singleton method
self inside a singleton method of object of class B => #<A::M::N::B:0x819cab4>

I have bolded the self in the output.So you can see that self keep changing to current object at any point of the running program.

NOTE : method always execute on current object referenced by self

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s