According to the documentation of the Ruby Set class, "== Returns true if the two sets are equal. The equality of each pair of elements is determined 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 are compared with equal:
require 'set' d1 = Date.today
But using my own objects, where did I code eql? a method to compare only certain attributes, I cannot 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}"
Exit from the above:
ipdet1 is equal to ipdet11: true ipdet2 is equal to ipdet12: true set 1 is equal to set2: false
Any ideas anybody?
source share