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:
By the way, ri is pretty informative too:
———————————————————— Object#eql?
obj == other => true or false
obj.equal?(other) => true or false
obj.eql?(other) => true or false
————————————————————————
Equality—At the +Object+ level, +==+ returns +true+ only if _obj_
and _other_ are the same object. Typically, this method is
overridden in descendent classes to provide class-specific meaning.
Unlike +==+, the +equal?+ method should never be overridden by
subclasses: it is used to determine object identity (that is,
+a.equal?(b)+ iff +a+ is the same object as +b+).
The +eql?+ method returns +true+ if _obj_ and _anObject_ have the
same value. Used by +Hash+ to test members for equality. For
objects of class +Object+, +eql?+ is synonymous with +==+.
Subclasses normally continue this tradition, but there are
exceptions. +Numeric+ types, for example, perform type conversion
across +==+, but not across +eql?+, so:
1 == 1.0 #=> true
1.eql? 1.0 #=> false
Oops, ignore my comments — you obviously know that.
I replied before reading your posting in full, my apologies.
No problem! I just got a little worried that maybe my post vanished away completely [blink/].
./alex
—
.w( the_mindstorm )p.
—
(http://themindstorms.blogspot.com)
Would you know of any site that compares Java and Ruby – where once can know the differences at a glance?
Hello,
Please give me an idea of reporting tool for ruby. I made a software and I need a reporting tool, besides text or html. I saw a post of yours with some pdf stuff.
Thank you,
Mir