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:

   

Saturday, July 23, 2011

Rails 3.0 Active Record -- Polymorphic association

Note: If you are clear with the concept of polymorphic association then you should read this article.

There is one drawback with polymorphic association which you might have faced while creating associations between models . The drawback is that activerecord allows only polymorphic ends to query the association. Lets say we have the following scenario.


Models we have


we can find courses which teacher and student has taken up


The above two statements works perfectly fine.

But what if we want to know all teachers or students who have taken up a particular course. Let say we query



The above two statements would throw error.

Lets see the course_memberships table


When we execute Course.first.teachers, ActiveRecord searches for teacher association in the CourseMembership model but it belongs_to :takable, not :teacher. Hence ActiveRecord throws error.
This can be solved through multiple solutions.

Solution 1:

Here is simple hack for this.


Now the non-polymorphic end query would work i.e. Course.first.teachers and Course.first.students. I just added a teacher and student association in CourseMembership and a condition in Course model.

Solution 2:

We can also solve this problem by defining teachers and students method in Course model but in this case we need to query N+ 1 times to get the result which would lower the performance.
N: number of teachers or students


Solution 3:

we can use includes method, its pretty cool as it takes the whole wad of data at one go from the database and eliminates N+1 query problem.
Here we are using secong degree association, hence we need to pass an hash with array as the value.

The slight problem with this solution is the query end up returning a lot of data to be converted into    ActiveRecord objects. Also if there are lot of rows in parent table, preloading will consume a lot of server memory.