From: Robert Klemme Date: 2009-09-05T19:40:05+09:00 Subject: Re: how to compare two object instances? is "m1.to_yaml.eql?(m2.to_yaml)" a good way? On 05.09.2009 11:52, Greg Hauptmann 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/