开发者

Ruby Set class: equality of sets

According to the Ruby Set class's documentation, "== Returns true if two sets are equal. The equality of each couple of elements is defined according to Object#eql?.

The essence of this can be demonstrated using Date objects, where sets containing different Date objects but with the same date compare to equal:

require 'set'
d1 = Date.today              # => Thu, 30 Sep 2010
puts d1.object_id            # => 2211539680
d2 = Date.today + 1          # => Fri, 01 Oct 2010
puts d2.object_id            # => 2211522320
set1 = Set.new([d1, d2])

d11 = Date.today             # => Thu, 30 Sep 2010
puts d11.object_id           # => 2211489080
d12 = Date.today + 1         # => Fri, 01 Oct 2010
puts d12.object_id           # => 2211469380
set2 = Set.new([d12, d11])

set1 == set2                 # => true

But using my own objects, where I've coded the eql? method to only compare certain attributes, I can't get it to work.

class IpDet

    attr_reader :ip, :gateway

    def initialize(ip, gateway, netmask,开发者_如何转开发 reverse)
        @ip = ip
        @gateway = gateway
        @netmask = netmask
        @reverse = reverse
    end

    def eql?(other)
        if @ip = other.ip && @gateway == other.gateway
            return true
        else
            return false
        end
    end
end



ipdet1 = IpDet.new(123456, 123457, 123458, 'example.com')
ipdet2 = IpDet.new(234567, 2345699, 123458, 'nil')

ipdet11 = IpDet.new(123456, 123457, 777777, 'other_domain.com')
ipdet12 = IpDet.new(234567, 2345699, 777777, 'example.com')

puts "ipdet1 is equal to ipdet11: #{ipdet1.eql?(ipdet11)}"
puts "ipdet2 is equal to ipdet12: #{ipdet2.eql?(ipdet12)}"


set1 = Set.new([ipdet1, ipdet2])
set2 = Set.new([ipdet11, ipdet12])

puts "set 1 is equal to set2: #{set1 == set2}"

The output I get from the above is:

ipdet1 is equal to ipdet11: true
ipdet2 is equal to ipdet12: true
set 1 is equal to set2: false

Any ideas anyone?


When you override eql?, you also always need to override hash such that if o1.eql?(o2) is true, o1.hash == o2.hash is also true.

For example your hash method could look like this:

def hash
  [@ip, @gateway].hash
end

Also you have a typo in your eql? method: @ip = other.ip should be @ip == other.ip.

Also a minor stylenote: if condition then true else false end is equivalent to just condition, so your eql? method can just be defined as

def eql?(other)
  @ip == other.ip && @gateway == other.gateway
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜