Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Last active Dec 19, 2015
Embed
What would you like to do?
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