Friday, August 28, 2009

booleans in ruby


Here's an interesting tidbit about the Ruby language: it has no native boolean type. The true and false keywords are actually instantiations of the TrueClass and FalseClass types. In ruby, things evaluates as a boolean like so:

- false and nil are false

- everything else is true

Sounds intuitive, but it may trip up programmers used to C-style languages to know that 0 (zero) evaluates to true. Here are some tests:





Tests run using Ruby 1.8.6.

Check out this Wikipedia article for more details about booleans in Ruby.