A “well-behaved” object in Ruby needs to understand the following:
What makes two Ruby objects “equal”?
And which version of “equal” (there are several in Ruby)?
And what makes an object usable as a
And is that the same thing that makes them
Because I can never seem to remember the specifics. And because my searching the Interwebs seems to find related, but not specific help. And because I’ve got this blurgh-thing… I might as well use it to help future-me (and maybe you?).
Gimme the gist
eql? for use as a
Hash key, and then
eql? method to
== for the expected developer ergonomics.
Something like this:
class Message attr_reader :body, :subject def initialize(subject:, body:) @subject = subject @body = body end def eql?(other) other.class == self.class && other.body == body && other.subject == subject end alias == eql? def hash body.hash ^ subject.hash end end
What to know more about the specifics, or how to also make these objects
Read on, friend…