Today I Learned

hashrocket A Hashrocket project

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
See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.