# How to compare two object instances? is "m1.to\_yaml.eql?(m2.to\_yaml)" a good way?

**URL:** <https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990>\
**Category:** Ruby\
**Created:** [September 5, 2009, 11:54am UTC](https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990 "2009-09-05T11:54:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 11:54am UTC](https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990/1 "2009-09-05T11:54:41Z")

</div>

Hi,

What’s an easy way to compare for equality two object instances (that  
haven’t been saved yet). Or in other words pretty much just two  
object instances. I’ve tried “==” and “eql?” on the objects directly  
and this doesn’t work.

How about: “m1.to\_yaml.eql?(m2.to\_yaml)” Is this the simplest way?

(i.e. leveraging the ability to serialize the object to a yaml string)

tks

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990/2 "2009-09-05T13:10:28Z")

</div>

Have you tried using obj\_1.hash == obj\_2.hash ?

Also, what do you mean by “saving” the object? Persisting it as a byte  
stream, a string, or in a database using ORM?

/ Vahagn

Greg H. wrote:

> Hi,
> 
> What’s an easy way to compare for equality two object instances (that  
> haven’t been saved yet). Or in other words pretty much just two  
> object instances. I’ve tried “==” and “eql?” on the objects directly  
> and this doesn’t work.
> 
> How about: “m1.to\_yaml.eql?(m2.to\_yaml)” Is this the simplest way?
> 
> (i.e. leveraging the ability to serialize the object to a yaml string)
> 
> tks

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990/3 "2009-09-05T13:42:24Z")

</div>

On 05.09.2009 11:52, Greg H. wrote:

> What’s an easy way to compare for equality two object instances (that  
> haven’t been saved yet).

Why do you believe equivalence has something to do with persistence?

> Or in other words pretty much just two  
> object instances. I’ve tried “==” and “eql?” on the objects directly  
> and this doesn’t work.
> 
> How about: “m1.to\_yaml.eql?(m2.to\_yaml)” Is this the simplest way?
> 
> (i.e. leveraging the ability to serialize the object to a yaml string)

You do not mention what classes these objects belong to. However, the  
proper way is to implement ==, eql? and hash in order to provide correct  
equivalence semantics. The easy way is to use Struct which will  
implement those methods for you based on the fields you define.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990/4 "2009-09-05T13:42:24Z")

</div>

I think the saving/persistence comment of mine diverted the focus of  
the question. Robert’s comment " the proper way is to implement ==,  
eql? and hash in order to provide correct equivalence semantics. The  
easy way is to use Struct which will implement those methods for you  
based on the fields you define." seem to be the key I was just  
asking for clarification re how the Struct can be used to save time in  
implementing…

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990/5 "2009-09-05T14:10:31Z")

</div>

2009/9/5 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Why do you believe equivalence has something to do with persistence?

It’s only the instances I want to compare of instances of a Rails  
model class which haven’t been saved (i.e. if they had been saved they  
may have then received their ID attribute values which may have been  
different). But yes my question is really generic to any given  
custom developed class I may happen to write.

> You do not mention what classes these objects belong to. However, the  
> proper way is to implement ==, eql? and hash in order to provide correct  
> equivalence semantics. The easy way is to use Struct which will implement  
> those methods for you based on the fields you define.

So I think you’re saying if I can convert the 2 instances of class X I  
want to compare to 2 instances of a Struct I create (with the same  
attributes of those in my class X) then I can just leverage the  
already implemented code in the Struct class for “==”? Is this what  
you were meaning?

tks

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990/6 "2009-09-05T14:00:33Z")

</div>

actually I just saw this posted on  
“[Reddit - Dive into anything](http://www.reddit.com/r/ruby/comments/9540i/ruby_developers_make_sure_you_define_eql_and_hash/)”  
that might be good…

```
def eql?(other)
    return false unless other.class == self.class
    return false unless other.instance_variables == 

```

self.instance\_variables  
self.instance\_variables.each do |var|  
self\_var = self.instance\_variable\_get(var)  
other\_var = other.instance\_variable\_get(var)  
return false unless self\_var.eql?(other\_var)  
end  
true;  
end

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990/7 "2009-09-05T14:22:13Z")

</div>

On 05.09.2009 13:07, Greg H. wrote:

> > proper way is to implement ==, eql? and hash in order to provide correct  
> > equivalence semantics. The easy way is to use Struct which will implement  
> > those methods for you based on the fields you define.
> 
> So I think you’re saying if I can convert the 2 instances of class X I  
> want to compare to 2 instances of a Struct I create (with the same  
> attributes of those in my class X) then I can just leverage the  
> already implemented code in the Struct class for “==”? Is this what  
> you were meaning?

No, I meant, if you want to be lazy you can implement your classes by  
using Struct. But your approach would work as well.

I don’t know whether and how AR implements ==, eql? and hash - chances  
are that they only compare the primary key in the database which  
probably would not help you - but this is just a guess.

If you need to convert your objects anyway, then you could also use  
Arrays, e.g.

a, b = [x,y].map {|o| [o.foo, o.bar, o.baz]}

puts( a == b )

Of course, things get more complicated if your class X does not only  
contain primitive data (such as Fixnum, String etc.) but other AR  
created objects as well. Maybe it’s better to then implement a to\_xyz  
method in the class which returns something which can be compared.  
There is a ton of options out there but for one I’m not a Rails user and  
then we do not have much detail about your use case.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 11:58pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-object-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174990/8 "2009-09-05T23:58:56Z")

</div>

2009/9/5 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> If you need to convert your objects anyway, then you could also use Arrays,  
> e.g.
> 
> a, b = [x,y].map {|o| [o.foo, o.bar, o.baz]}
> 
> puts( a == b )

Thanks Robert - this looks like a good way to go then.
