Friday, February 1, 2013

How Super keyword works with inhertance and mixings in ruby

Today i decided to find how super keyword works in inheritance and including a module in  ruby and how the ladder of function calling build. i made couple of examples in this process, here they are :-

Example 1 :-

class Test

def show
puts "i am in Test show"
end
end

class Test1 < Test

def show
puts "i am in Test1 show"
super
end
end

t = Test1.new
t.show

outputs:-
i am in Test1 show
i am in Test show

its works as expected :-)

Example 2 :-

Some rubiest ( atleast me) think that super calls the parent class method.
Now i create a module have the same name function in it. so that method should not be called. Here is the example :-

module A
 def show
 puts " i am in module A show method"
end
end

class Test

def show
puts "i am in Test show"
end
end

class Test1 < Test

include A
def show
puts "i am in Test1 show"
super
end
end

t = Test1.new
t.show

Output :-
i am in Test1 show
i am in module A show method


Explanation :-

Here as per my simple terminology function i calls it ladder concept of function calling. So when our class loads it creates a function calling ladder as i shown below (its completely my way of understanding the concept ).  when our Test1 starts loading our interpreter see it is inherited from Test it goes to Test class and start making the ladder for show function and puts Test show at level 0. Now control comes back to Test1 class here we include the module A now control goes there and see ohhh here another show method is here so interpreter inserted the show method Just after the Test1 class show method.

Example 1                                                           Example 2
(show method ladder)                             (show method ladder)


| Test1 show|                                           |    Test 1 show   |
----------------                                            ---------------------
|  Test show |                                           | module A show |
----------------                                            ---------------------
                                                               |    Test Show      |
                                                                ----------------------

So super doesn't call the parent class method it just calls the next level function which we make as per our ladder ruby concept.



hmmmmmmmm

What will happen if i write super keyword inside module A show method. As per our ladder concept it should call the Test show

Example 3 :-

module A
 def show
 puts " i am in module A show method"
 super
end
end

class Test

def show
puts "i am in Test show"
end
end

class Test1 < Test

include A
def show
puts "i am in Test1 show"
super
end
end

t = Test1.new
t.show

Output :-
i am in Test1 show
i am in module A show method
i am in Test show


So My Ladder concept works here :-)



hmmmm


Example 4 :-
What will happen if i  include the module after the definition of show method like below

module A
 def show
 puts " i am in module A show method"
 super
end
end

class Test

def show
puts "i am in Test show"
end
end

class Test1 < Test


def show
puts "i am in Test1 show"
super
end
include A 

end

t = Test1.new
t.show

Output :-
i am in Test1 show
i am in module A show method
i am in Test show


Last Example

What will happen if i have another module similar to A

module A
 def show
 puts " i am in module A show method"
 super
end
end

module B
 def show
 puts " i am in module B show method"
 super
end
end

class Test

def show
puts "i am in Test show"
end
end

class Test1 < Test


def show
puts "i am in Test1 show"
super
end
include A 
include B
end

t = Test1.new
t.show

Output :-
i am in Test1 show
i am in module B show method
i am in module A show method
i am in Test show


 Explanation :-

Show Function ladder

                            |    Test1 Show    |
                             ---------------------
                            | module B show |
                            ----------------------
                            | module A show |
                             ---------------------
                            |    Test Show     |
                            ----------------------

 Here module B given preference because our interpreter comes to intereact with Line include B after include A and as per our ladder rule it insert in the ladder just after the class method

No comments: