DEV Community

loading...

Discussion on: Value objects in Ruby

Collapse
citizen428 profile image
Michael Kohl

Note that if you have pry-doc installed, you can also use show-doc pizza_day.== in Pry , which will output the following

From: object.c (C Method):
Owner: BasicObject
Visibility: public
Signature: ==(arg1)
Number of lines: 28

Equality --- At the Object level, == returns
true only if obj and other are the same object.
Typically, this method is overridden in descendant classes to provide
class-specific meaning.

Unlike ==, the equal? method should never be
overridden by subclasses as it is used to determine object identity
(that is, a.equal?(b) if and only if a is the
same object as b):

  obj = "a"
  other = obj.dup

  obj == other      #=> true
  obj.equal? other  #=> false
  obj.equal? obj    #=> true

The eql? method returns true if obj and
other refer to the same hash key.  This is used by Hash to test members
for equality.  For objects of class Object, eql?
is synonymous with ==.  Subclasses normally continue this
tradition by aliasing eql? to their overridden ==
method, 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
Enter fullscreen mode Exit fullscreen mode

This is quite a bit more convenient than using owner and then having to look up the documentation online.

Collapse
codeandclay profile image
Oliver Author • Edited

Yes, a lot more convenient. Thanks. I wish I'd known about it before.

Forem Open with the Forem app