1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

TrueClass, FalseClass and NilClass Quick Reference

By

TrueClass, FalseClass and NilClass Quick Reference

As everything in Ruby is an object, even things like true and false are classes. There's also a nil class to represent nil (in some contexts, nil represents neither true or false). The classes that represent these are TrueClass, FalseClass and NilClass.

These classes are rather unique in the Ruby environment in that you cannot instantiate them. There is a single object of type TrueClass and it is referred to as true. The same goes for the FalseClass and the NilClass (false and nil respectively). And not only that, but these instances are represented by special and unique object IDs. Object IDs are a bit of Ruby magic that you'll never see unless you delve into the realm of C, but suffice it to say, these are specially created objects with fixed object IDs.

What is Truth?

We're not straying into a philosophical argument here, programming languages must have strict rules to define what is and what is not truthful. For example, in C any 0 value is false, everything else is true. This is used very often in conditionals and while loops, 0 is just assumed to be false so there's no need for a comparison. Coincidentally (or, more accurately, intentionally) this is how most CPUs work, so it closely mirrors the machine C is abstracting. Yet other languages have various thoughts on what exactly is truthful. In Ruby, FalseClass and NilClass instances (in other words, false and nil) are false, and everything else is true. I know I've accidentally designed a C-like conditional statement in Ruby before assuming 0 would be false, but it isn't, 0 is true in Ruby.

TrueClass and FalseClass

While these special boolean classes generally are not used on their own (they are sent to messages, they don't receive messages), there are a few operators you can perform on them. These are the three boolean operators and, or and xor. Much like the bitwise boolean operators for integers, but only operating on a single bit of information.

  • & - Produce true if the object and its operand are both true. If called on true, this will produce true if the operand is also true. So true & true will produce true. However, if called on a FalseClass object, it will always produce false. So false & something_else will always produce false, there is nothing that can be put on the right side of that expression to make it true.
  • | - If either operand is true, produce true. If called on a TrueClass object, this will always return true. It doesn't matter how false the right side operand is, one of them is always true. So true | something_else will always produce true. However, false | something_else will only return true if the second operand is true.
  • ^ - This operates the same as the | or operator, except if both are true, it produces false. So for the FalseClass object, it will produce true if the operand is true. However, it will only produce true on a TrueClass object if the operand is false. For example, false ^ true will produce true, but true ^ true will produce false, since both are true.

NilClass

The NilClass is the same as FalseClass, but with a few additions. Since the nil object is used in place of an "empty object," the NilClass instance can convert itself into various empty objects.

  • to_a - Produce an empty array object.
  • to_c - Produces the complex number 0 + 0i.
  • to_h - Produces an empty hash.
  • to_i - Produces 0.
  • to_r - Produces the rational number 0/1.
  • to_s - Produces an empty string.
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. TrueClass, FalseClass and NilClass Quick Reference

©2014 About.com. All rights reserved.