On Oct 17, 2005, at 6:24 PM, David Corbin wrote:

> Without getting into the details of which equality method (eql?,  
> equal?, ==),
> I'm curious about how such a method should be written for class  
> where you're
> trying to keep the interface appropriatley private.
>
> Consider:
>
> class Foo
>     def initialize(color)
>         @color = color
>     end
>     .
>     .
>     .
> end
>
> If I do not wish to expose color via an attr_reader, how do I write an
> equality method for this class?

Use a protected accessor:

class Foo
     def initialize( color )
         @color = color
     end

     attr_reader :color
     protected :color

     def ==( other )
         @color == other.color
     end
end

Or cheat:

class Foo
     def initialize( color )
         @color = color
     end

     def ==( other )
         @color == other.instance_eval { @color }
     end
end

Hope that helps.

James Edward Gray II