![]() |
Zero is true ... whoda thunk?
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 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.687 / Virus Database: 448 - Release Date: 5/16/2004 |
Re: Zero is true ... whoda thunk?
il Tue, 18 May 2004 17:15:26 -0400, "Richard Lionheart"
<NoOne@Nowhere.com> ha scritto:: >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 :) |
Re: Zero is true ... whoda thunk?
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 |
Thanks to both Mark and Gabriele
Hi,
Thanks for your responses. Thanks especially to you, Mark, for the reference in Programming Ruby. You folks are great. Regards, Richard "Richard Lionheart" <NoOne@Nowhere.com> wrote in message news:4oudnTkSkNU64DfdRVn-hA@comcast.com... > 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 > > > > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.687 / Virus Database: 448 - Release Date: 5/16/2004 > > --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.687 / Virus Database: 448 - Release Date: 5/16/2004 |
Re: Zero is true ... whoda thunk?
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" <NoOne@Nowhere.com> wrote... > 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> |
| All times are GMT. The time now is 09:22 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.