ruby operators <, >, === etc.
ruby -v 2.2.2
Different implementations of some of these operators can be used to do some cool things.
The #< implementation on Module for example.
> Module < BasicObject
=> true
> Object < Class
=> false
#== and #=== are implemented the same on Fixnum or BigDecimal, but on Module
> a === b
evaluates to true if b is an instance of a or a's descendants
> (1..10) === 5
=> true
> 5 === (1..10)
=> false
> 'str' === String
=> false
> String === 'str'
=> true
This is the case-equality operator and this behavior can be overridden to customize case statements. An example of overriding.
class Range
def ===(o)
puts 'nope sucka'
end
end
case 5
when(1..10)
'This would normally work'
else
end
=> nope sucka
Tweet