G
Gary Shea
While writing an acceptance test, I needed to compare two sets of sets
of integers:
require 'set'
a = Set.new([Set.new([3,4]), Set.new([5,6])])
b = Set.new([Set.new([3,4]), Set.new([5,6])])
puts #{a == b}\n"
prints out 'false'.
Apparently the built-in Set library uses Hash#includes? to determine if
an object is in the rhs set. This method appears to use object id, so
set equality is based on set member object id.
A naive #==(set) that works the way I think it should is pretty simple:
def Set.my_eql?(set)
...
# this is the only part of my_eql? that differs from #==(set),
# replacing the line 'set.all? { |o| hash.include?(o) }'
set.all? { |o|
@hash.each_value do |my_o|
if my_o == o
last true
end
false
end
}
end
I'm sure this has come up before... is there a better solution? This
one is fine for my purposes, but I would be nice there was a packaged
solution out there.
Gary
of integers:
require 'set'
a = Set.new([Set.new([3,4]), Set.new([5,6])])
b = Set.new([Set.new([3,4]), Set.new([5,6])])
puts #{a == b}\n"
prints out 'false'.
Apparently the built-in Set library uses Hash#includes? to determine if
an object is in the rhs set. This method appears to use object id, so
set equality is based on set member object id.
A naive #==(set) that works the way I think it should is pretty simple:
def Set.my_eql?(set)
...
# this is the only part of my_eql? that differs from #==(set),
# replacing the line 'set.all? { |o| hash.include?(o) }'
set.all? { |o|
@hash.each_value do |my_o|
if my_o == o
last true
end
false
end
}
end
I'm sure this has come up before... is there a better solution? This
one is fine for my purposes, but I would be nice there was a packaged
solution out there.
Gary