Daily Archives: June 25, 2006

A parallel of equality in 2 worlds (Ruby and Java)

Here is a short presentation of equality methods in Ruby and Java:

Ruby Java
Object#equal? ==
Object#eql? Object.equals
Object#== Object.equals
Object#=== N/A

So, in both Ruby and Java we have an instance reference equality or identity (Object#equal?, respectively the == operator).

Another similarity is for objects used as keys in maps (hashes). If you want to customize their behavior, in both Ruby and Java will have to override Object#eql? and Object#hash, respectively Object.equals() and Object.hashCode(). And that would be it with the similarities.

In Ruby world, Object#eql? and Object?== are named equivalence. I haven’t been able to find out the reasons for having both Object#eql? and Object?== and the only example I have found is the following:

2.eql? 2.0 => false
2 == 2.0 => true

Ruby has another equality method Object#=== and this one is used only in case statements.

Reference:

category: , , ,
Advertisement

5 Comments

Filed under Uncategorized