Thanks Cliff and Steve - legends!

  def eql?(other)
    [name].eql?([other.name])
  end
  def hash
    [name].hash
  end

http://www.ruby-forum.com/topic/165653

Sonia Hamilton.



Steve Hoeksema wrote:
> Try "eql?"
>
> see 
> http://stackoverflow.com/questions/372652/what-are-the-ruby-gotchas-a-newbie-should-be-warned-about/392774#392774
>
> On Fri, Aug 28, 2009 at 4:02 PM, Sonia Hamilton <[email protected] 
> <mailto:[email protected]>> wrote:
>
>
>     Hi everyone,
>
>     My question is more about Ruby than Rails...
>
>     I've got arrays of objects and I'm trying to do array differences.
>     What
>     do I need to override in my objects for array differences to work?
>     (where I want equality based on attributes not object ids) I
>     thought it
>     was == but it doesn't seem to be working - please hit me with a clue
>     stick :-)
>
>     A bit more detail:
>
>     Using simple arrays you can do array differences (and additions,
>     unions,
>     etc):
>
>      >> a1 = %w[a b]
>      >> a2 = %w[a c]
>      >> a1 - a2
>     => ["b"]
>
>     Using a class like this:
>
>     class Host
>      attr_accessor :name
>      def initialize(hostname)
>          @name = hostname + '.company.com.au <http://company.com.au>'
>      end
>      def ==(h)
>        @name == h.name <http://h.name>
>      end
>     end
>
>     I would've thought differences of arrays of Hosts would then work,
>     but no:
>
>      >> hosts = [ Host.new('a'), Host.new('b') ]
>     => [#<Host:0xb705b5ec @name="a.company.com.au
>     <http://a.company.com.au>">, #<Host:0xb705b588
>     @name="b.company.com.au <http://b.company.com.au>">]
>      >> hosts2 = [ Host.new('a'), Host.new('c') ]
>     => [#<Host:0xb7057fc8 @name="a.company.com.au
>     <http://a.company.com.au>">, #<Host:0xb7057f64
>     @name="c.company.com.au <http://c.company.com.au>">]
>      >> p hosts - hosts2
>     [#<Host:0xb705b5ec @name="a.company.com.au
>     <http://a.company.com.au>">, #<Host:0xb705b588
>     @name="b.company.com.au <http://b.company.com.au>">]
>
>     The == is however working:
>
>      >> h1 = Host.new('a')
>     => #<Host:0xb7086800 @name="a.company.com.au
>     <http://a.company.com.au>">
>      >> h2 = Host.new('a')
>     => #<Host:0xb7084a28 @name="a.company.com.au
>     <http://a.company.com.au>">
>      >> h1 == h2
>     => true
>
>     --
>     Sonia Hamilton.
>
>
>
>
>
>
> -- 
> Steve Hoeksema
> +61 404 938 816
>
> >

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to