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

Boolean Expressions

By

A boolean expression is an expression used in a conditional or loop construct that decides which path the Ruby interpreter should take. In other words, Ruby will ask itself "if this boolean expression is true do this, otherwise do that." The boolean expression is what allows a Ruby control structure to make that decision.

Since this is a boolean expression, this implies that the expression evaluates to either true or false. In Ruby, the true and false values are represented by two keywords and classes. The true keyword references an instance of the TrueClass class. The false keyword references an instance of the FalseClass class. Both of these classes are special, you can't instantiate them and all occurrences of the true and false class refer to the same two objects created by the Ruby interpreter when it starts.

Another related class and keyword in Ruby is the nil keyword and NilClass class. The nil keyword produces the same type of object: a single special instance of the NilClass created by the Ruby interpreter when it starts.

Also, recall that all expressions in Ruby evaluate to objects. Simple literal expression like 10 or "hello" evaluate to the objects that the literals generate. Method calls evaluate to the object the method returns, and if it returns nothing then it evaluates to nil. The point here is that all expressions evaluate to a single object. You can see this object by typing some expressions into IRB, the line that gets printed after you hit enter is what object was obtained by evaluating the expression.


irb(main):001:0> 10
=> 10
irb(main):002:0> "test"
=> "test"
irb(main):003:0> true
=> true
irb(main):004:0> true.class
=> TrueClass
irb(main):005:0> false.class
=> FalseClass

Truth

There's only one thing you need to remember about truth in Ruby: everything is true in Ruby except for false and nil. That is, any object that is tested for "truthiness" will be true unless it's the false or nil objects. Every single other object is true. This includes the number 0, which in many languages (such as C and C++) is false. Don't forget, 0 is true in Ruby.

To do some experimentation, we'll define a method called true? that will print whether the passed object (or the object obtained after evaluating the expression in the method call) is true or not. This is not very useful in actual code (to test "truthiness," you generally just use an if statement), but this should be useful when exploring the language.


#!/usr/bin/env ruby

def true?(o)
  if o
    puts "#{o.inspect} is true"
  else
    puts "#{o.inspect} is false"
  end
end

true?(10)
true?(0)
true?("test")
true?(true)
true?(false)
true?(nil)

Comparison Operators

Boolean expressions usually involve one or more comparison operators. A comparison operator takes two objects (or objects obtained by evaluating an expression), compares them in some way and evaluates to either true or false. This is the primary way that boolean expressions are constructed.

The most used of these comparison operators is the equality operator: == (two equals signs). This should evaluate to true if the two objects passed to it are equal. The word "should" is used here because the == operator is implemented as methods to each class. How "equality" is defined depends on the objects being compared. However, there won't be many surprises here. If you compare two strings, the operator will be evaluated as true if the strings are equal. If you compare two numbers, it will be true if they're equal. If you compare two objects of different types, the behavior of the operator isn't well defined.

Also, don't confuse the assignment (=, single equals sign) with the assignment operator (==, double equals sign). Some languages infer which operator is needed, and there is only one operator for both functions. Other languages use an operator like := for assignment, and = for equality. It's easy to get confused, and this error won't throw any red flags in most cases. It will silently fail and you'll be wondering why your code doesn't work. Some have even gone as far as putting objects that cannot be assigned to on the left side of the operator. For example, instead of saying a == 10, say 10 == a. If you type 10 = a by accident, you cannot assign to 10, so there will be an error.

There are a number of comparison operators you can use:

 

  • a == b : True if a is equal to b.
  • a != : True if a is not equal to b.
  • a > b : True if a > b.
  • a < b : True if a < b.
  • a >= b : True if a >= b.
  • a <= b : True if a <= b.
  • a =~ b : Where a is a string and b is a regular expression, return true if the regular expression matches within a.
  • a !~ b : Where a is a string and b is a regular expression, return true if the regular expression does not match within a.

 

  1. About.com
  2. Technology
  3. Ruby
  4. Control Structures
  5. Boolean Expressions

©2014 About.com. All rights reserved.