Thursday 15 November 2012

Equality method

Sometimes i get caught by the equality methods and thus have decided to concrete it so that it stays
in me for years to come


 eql? 

 => whether the contents are same or equal
       a = "cat" ; b = "dog" ; c = "cat"

a.eql? b => false ( that is obvious ... how can a cat be same as dog)

a.eql? c => true ( it definitely make sense as long as the content are same)

a == c  => true

N.B : Be careful while using == method as because Numeric type perform type conversion across.

Say for example
         2 == 2.0 => true
but
        2.eql? 2.0 => false


equal?

a = ["cat", "dog"]; b = ["cat", "dog"]

a.object_id => 12346
b.object_id => 34567

Since a and b objects have same content but the message(object_id) called on the receivers(a and b) returns different values, so the equal? method checks for the same object id.

a = :cat; b = :cat

a.object_id => 123
b.object_id => 123

In this case both a and b are referring to the same object.

Tuesday 6 November 2012