Richard Lionheart wrote:

>Hi,
>
>I can't find anything in Programming Ruby to suggest why zero is treated as
>true,  as happens in lines and 12 in the following toy program.  Is it
>perhaps because Ruby has no boolean type,  so zero gets converted to a
>string, or something?  Aside from an explantion,  can you cite a relevant
>page in the Thomas&Hunt book?
>
>  
>
The page you want is 223. The relevant passage is

"Ruby predefines the globals false and nil. Both of these values are 
treated as being false in a boolean context. All other values are being 
true."

So zero isn't being converted into anything but it is considered to be 
true since the only values which are considered to be false are nil 
values and values that are explicitly false.

One advantage of this is performance. Both NilClass and FalseClass are 
singleton classes so checking whether an object is nil or false is very 
easy, while allowing 0 to be false would require testing the type and 
value of the object.

--
Mark Sparshatt