Zero is true ... whoda thunk?

R

Richard Lionheart

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?

Thanks in advance,
Richard

def count_exes(l)
n = l.count "x"
end

DATA.each_line do |line|
print line.chomp
if n = count_exes(line) then print "\t(#{n} exes in line) [Pgm line 7]"
end
if (n = count_exes(line)) > 0 then print "\t(additional note for
ex-lines)" end
puts
end

puts "0 is " + (0?"true":"false") + " [Pgm line 12]"

__END__
Two 'xx's
Three 'xxx's
No ekses
 
G

gabriele renzi

il Tue, 18 May 2004 17:15:26 -0400, "Richard Lionheart"
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?

don't know the page, but you can safely assume that every object is
true in ruby, excluding nil and false. And, well, false and true seem
booleans to me :)
 
M

Mark Sparshatt

Richard said:
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.
 
R

Richard Lionheart

Hi,

Thanks for your responses. Thanks especially to you, Mark, for the
reference in Programming Ruby.

You folks are great.

Regards,
Richard
Richard Lionheart said:
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?

Thanks in advance,
Richard

def count_exes(l)
n = l.count "x"
end

DATA.each_line do |line|
print line.chomp
if n = count_exes(line) then print "\t(#{n} exes in line) [Pgm line 7]"
end
if (n = count_exes(line)) > 0 then print "\t(additional note for
ex-lines)" end
puts
end

puts "0 is " + (0?"true":"false") + " [Pgm line 12]"

__END__
Two 'xx's
Three 'xxx's
No ekses
 
D

Dave Burt

The Pickaxe Book reference has been given, but I'd like to point you to
another book which deals brilliantly with this sticky issue:

Why's (Poignant) Guide to Ruby (Chapter 4)
<http://www.poignantguide.net/ruby/chapter-4.html>

Richard Lionheart said:
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?
<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top