Created
March 20, 2012 15:56
-
-
Save wjdix/2137423 to your computer and use it in GitHub Desktop.
ruby stdlib set error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point | |
attr_reader :x, :y | |
def initialize(x, y) | |
@x, @y = x, y | |
end | |
def ==(other) | |
x == other.x && y == other.y | |
end | |
end | |
ary1 = [Point.new(1, 3), Point.new(2, 4)] | |
ary2 = [Point.new(1, 3), Point.new(3, 5)] | |
ary1.select{|item| ary2.include? item} #==> [<#point x=1 y=3>] | |
Set.new(ary1).intersection(Set.new(ary2)).to_a #==> [] |
Ahh, the old adage is true once again. If you think there is a problem with the language, you are probably wrong.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The equality of each couple of elements is determined according to Object#eql? and Object#hash, since Set uses Hash as storage. (http://www.ruby-doc.org/stdlib-1.9.3/libdoc/set/rdoc/Set.html) Since they're different object refs, each points .hash() differs and thus no Set intersection.