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
=> false
Nothing interesting here, move along.
"foo" == E.new
In E: calling == against foo
=> false
Calling ==
on String
triggers ==
on other
object of Exception
class.
42 == A.new
In A: calling == against 42
=> false
Calling ==
on Fixnum
triggers ==
on other
object.
42 == E.new
In E: calling == against 42
=> false
Calling ==
on String
triggers ==
on other
object of Exception
class.
- Why calling
==
onString
orFixnum
triggers calling==
onother
object at all? - Why calling
==
onString
works differently depending on theother
object class?
In Ruby 1.9+ calling ==
on other
object is not triggered, bug has been fixed.