Search This Blog

Thursday, August 16, 2012

Ruby -metaprogramming,send,self and stuff !

Function overriding and overloading:


Function overloading
I will tell how the interpreter sees this code,
  • inside the class we have a function called 'f' defined in line 2
  • now the same function is defined again at line 5. So the previous definition of function 'f' gets overridden completely.. in the sense the previous definition is lost !! Note that this happens even if the number of parameters vary! In C both functions will exist.
  • In line 8 the function 'f' is redefined once again . So obviously the previous definition (in line 5) is forgotten or lost. 
  • So when you call f from your main program, the 'f' defined in line 8 gets executed.
Wondering how a.f() in line 15 got executed??
That's because 'f' in line 8 has a default parameter . When you have a default parameter you can call the function even without passing any value for that parameter. However if you specify some value the value you sent will override the default value.
so line 15 produces "raj" as output and line 16 produces "kumar" as output.

Ruby doesn't support function overloading like C++. However Ruby has the following implementation which is similar to function overloading,
def fun(*args)
end
this function can be called using any number of arguments and any type of arguments.
args is an array which holds these arguments. In a way this is function overloading.
Function overriding happens the same way as in other languages like C++ or Java.

Self object explained:


self object in ruby
self is always unique at a given point of program execution. It holds the current object of execution. Understanding self object clearly will give you deeper insight on ruby itself. Without sound knowledge about self it is difficult to understand how a completely object oriented language like Ruby works. self is analogous to this keyword in C++. I wrote above program to see the value of self at various places in a simple ruby program.


Output of self object demo
From this output we can see that, inside a class the self object can be a the class itself or the particular instance depending upon where it is used.
Inside an instance method self is the instance object
Inside class method or body of class it is the class itself(here A)
In the main program and methods outside classes, self is main object.

Send method in Ruby: 
send method and opening a class
send is a way of calling methods. It takes the method name as the first parameter and the method's arguments as remaining parameters.
In the above example, function 'f' is called using send.
Tip: self.class.send can be used to call the class method from an instance!


Output of send demo

Meta programming:


method_missing --> a function which gets called when a method is not available in the class. For this to work for class methods define method missing as class method . 
In the code i have shown above i have defined method_missing both as class method(using self keyword) and instance method.
In the instance method, method_missing , dynamically new methods are created!
In this example,
  • mul_3(4) in line 16 will pass the control to method_missing in line 5
  • now a new lambda is created which does the multiplication
  • define_method is called using send in line 10, which defines a method of name m and block l.
  • finally the created method is called in line 11 using send. a is the argument passed.
Note: define_method is an inbuilt method which is used to dynamically create methods. It can be used as class method only and not as instance method

No comments:

Post a Comment