Ruby has a simple definition of truth. Any value that is not
nil
or
the constant
false
is true. You'll find that the library
routines use this fact consistently. For example,
IO#gets
,
which returns the next line from a file, returns
nil
at end of
file, enabling you to write loops such as:
while line = gets
# process line
end
|
However, there's a trap here for C, C++, and Perl
programmers. The number zero is
not interpreted as a false
value. Neither is a zero-length string. This can be a tough habit to
break.