Thursday, February 16, 2012

No Method Overloading in Ruby

In Ruby there is no concept of method overloading. One method can have different names but two different method cannot have same names even if the number of arguments are same.


The second method overrides the first and hence there is no existence of the first method with arguments.

In fact, there is no need of method overloading in Ruby. Apparently, method overloading is required for two reasons.
First, methods with same name and accepting different number of arguments.
Second, methods with same name and accepting same number of arguments but different data types.

Both of the above two requirements can be achieved in Ruby.
First, as Ruby method arguments can be declared with default values and hence these arguments can be omitted on method invocation.
Second, as Ruby methods can accept arguments of any class.



Tuesday, February 14, 2012

Singleton class. Singleton method Vs Class method.

Every object is associated with two classes. One is which we can get using class method of object(eg: "hello".class) and the other is called the eigenclass(or singleton class).

The singleton methods of an object are the instance methods of the anonymous eigenclass associated with that object.

To open up an singleton class of an object str, use class << str

Eg:


 Thus, singleton class is an anonymous class associated with every object.


A method added to a single object rather than to a class of objects is known as singleton method. Also the class methods on a class are nothing more than singleton methods on the Class instance that represent that class.

Eg:


On the other hand, class methods are invoked only through the constant refering to the Class object.

Eg: