Ruby supports all the standard boolean operators and introduces the
new operator defined?.

Both ``and'' and ``&&''
evaluate to true only if both operands are
true. They evaluate the second operand only if the first is true
(this is sometimes known as ``short-circuit evaluation''). The only
difference in the two forms is precedence (``and'' binds lower than
``&&'').

Similarly, both ``or'' and ``||''
evaluate to true if either operand
is true. They evaluate their second operand only if the first is
false. As with ``and'', the only difference between ``or'' and
``||'' is their precedence.

Just to make life interesting, ``and'' and ``or'' have the
same precedence, while ``&&'' has a higher precedence than
``||''.

``not'' and ``!''
return the opposite of their operand (false if the
operand is true, and true if the operand is false). And, yes, ``not''
and ``!'' differ only in precedence.

All these precedence rules are summarized in Table
18.4 on page 219.

The defined?
operator returns nil if its argument (which can be
an arbitrary expression) is not defined, otherwise it returns a
description of that argument. If the argument is yield,
defined? returns the string ``yield'' if a code block is
associated with the current context.

defined? 1

�

"expression"

defined? dummy

�

nil

defined? printf

�

"method"

defined? String

�

"constant"

defined? $&

�

nil

defined? $_

�

"global-variable"

defined? Math::PI

�

"constant"

defined? ( c,d = 1,2 )

�

"assignment"

defined? 42.abs

�

"method"

In addition to the boolean operators, Ruby objects support comparison
using the methods ==, ===, <=>, =~, eql?,
and equal? (see Table 7.1 on page 79). All but <=>
are defined in class Object but are often overridden by
descendents to provide appropriate semantics. For example, class
Array redefines == so that two array objects are equal if
they have the same number of elements and corresponding elements are
equal.

Common comparison operators

Operator

Meaning

==

Test for equal value.

===

Used to test equality within a when clause of a
case statement.

<=>

General comparison operator. Returns -1, 0, or +1,
depending on whether its receiver is less than, equal to, or
greater than its argument.

<, <=, >=, >

Comparison operators for less than, less than or
equal, greater than or equal, and greater than.

=~

Regular expression pattern match.

eql?

True if the receiver and argument have both the same
type and equal values. 1 == 1.0 returns true,
but 1.eql?(1.0) is false.

equal?

True if the receiver and argument have the same
object id.

Both == and =~ have negated forms, != and !~.
However, these are converted by Ruby when your program is read.
a!=b is equivalent to !(a==b),
and a!~b is the
same as !(a=~b). This means that if you write a class that
overrides == or =~ you get a working != and !~
for free. But on the flip side, this also means that you cannot define
!= and !~ independent of == and =~, respectively.

You can use a Ruby range as a boolean expression.
A
range such as exp1..exp2 will evaluate as false
until exp1 becomes true. The range will then evaluate as true
until exp2 becomes true. Once this happens, the range resets,
ready to fire again. We show some examples of this
on page 82.

Finally, you can use a bare regular expression as a boolean
expression. Ruby expands it to $_=~/re/.