Skip to content

Instantly share code, notes, and snippets.

@wjdix
Created March 20, 2012 15:56
Show Gist options
  • Save wjdix/2137423 to your computer and use it in GitHub Desktop.
Save wjdix/2137423 to your computer and use it in GitHub Desktop.
ruby stdlib set error
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 #==> []
@joshuaknox
Copy link

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.

@wjdix
Copy link
Author

wjdix commented Mar 20, 2012

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