This gist shows buggy behavior of == in Ruby 1.8.7. This affects case equality (===) too because by default === calls == internally unless redefined in specific class (like Range or Regexp).
class A
def ==(other)
puts "In A: calling == against #{other}"
super
end
end
class E < Exception
def ==(other)
puts "In E: calling == against #{other}"
super
end
end"foo" == A.new
=> falseNothing interesting here, move along.
"foo" == E.new
In E: calling == against foo
=> falseCalling == on String triggers == on other object of Exception class.
42 == A.new
In A: calling == against 42
=> falseCalling == on Fixnum triggers == on other object.
42 == E.new
In E: calling == against 42
=> falseCalling == on String triggers == on other object of Exception class.
- Why calling
==onStringorFixnumtriggers calling==onotherobject at all? - Why calling
==onStringworks differently depending on theotherobject class?
In Ruby 1.9+ calling == on other object is not triggered, bug has been fixed.