Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Last active December 19, 2015 13:42
Show Gist options
  • Save thiagoa/434b874c69abe1ad9e29 to your computer and use it in GitHub Desktop.
Save thiagoa/434b874c69abe1ad9e29 to your computer and use it in GitHub Desktop.
class Obj
attr_reader :a
def initialize(a)
@a = a
end
def ==(other)
a == other.a
end
alias_method :eql?, :==
def hash
a.hash
end
end
o1 = Obj.new(:a)
o2 = Obj.new(:a)
o3 = Obj.new(:a)
a = [[o1], [o2], [o3]]
p a.inject(:&) # Recognizes and returns the common element
#------------------------------------
class Obj
attr_reader :a
def initialize(a)
@a = a
end
def ==(other)
a == other.a
end
end
o1 = Obj.new(:a)
o2 = Obj.new(:a)
o3 = Obj.new(:a)
a = [[o1], [o2], [o3]]
p a.inject(:&) # Does not recognize common elements.. returns []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment