[Ruby] eql? and hash on Sets

Ryan Davis ryand-ruby at zenspider.com
Sat Nov 24 16:58:25 PST 2007


On Nov 24, 2007, at 14:22 , Scott Windsor wrote:

> You're incorrect that Set#eql? is implemented by Set.  When you call
> Set#eql?, you're actually calling the inherited Object#eql? - which
> if you compare two different objects, they will not match.  You can,
> however, use Set#== to determine if two sets are equal...

 From set.rb:

   def ==(set)
     equal?(set) and return true

     set.is_a?(Set) && size == set.size or return false

     hash = @hash.dup
     set.all? { |o| hash.include?(o) }
   end

   def hash	# :nodoc:
     @hash.hash
   end

   def eql?(o)	# :nodoc:
     return false unless o.is_a?(Set)
     @hash.eql?(o.instance_eval{@hash})
   end



More information about the Ruby mailing list