On Mon, Jun 17, 2013 at 10:52 AM, Thom T. <lists / ruby-forum.com> wrote:

> How do I compare two objects in Ruby, considering only attributes
> values?
>
> For example:
>
>   class Foo
>       attr_accessor :bar, :baz
>   end
>
>   lorem = Foo.new
>   ipsum = Foo.new
>
>   lorem.bar = 1
>   lorem.baz = 2
>
>   ipsum.bar = 1
>   ipsum.baz = 2
>
>   puts lorem == ipsum # false
>
> Both objects belongs to the same class and also have the same attributes
> values.
>
> Thanks.
>
>
I'd do it like this

class Foo
  attr_accessor :bar, :baz
  def ==(foo)
    foo.kind_of?(self.class) && bar == foo.bar && baz == foo.baz
  end
end

Only define <=> if your foos are ordered.