Overridden == in Ruby

25 Dec 2012

I am in a situation, where overidding `==` produces a beautiful structure. But I'm not gonna explain it here.

I've run some experiments to verify that what I do will work. Here is the snippet:

class A def ==(other) other.instance_of?(A) end end puts "A.new == A.new: #{A.new == A.new}" a = A.new b = A.new arr = [a, b, "a", "b"] puts "Include?(A.new): #{arr.include?(A.new)}" puts "After deleting all A.new: #{arr.delete_if { |x| x == A.new }.inspect}" puts "Final: #{arr.inspect}" # Output # A.new == A.new: true # Include?(A.new): true # After deleting all A.new: ["a", "b"] # Final: ["a", "b"]

Give it a kudos