Zero is true ... whoda thunk?

Discussion in 'Ruby' started by Richard Lionheart, May 18, 2004.

  1. 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
     
    Richard Lionheart, May 18, 2004
    #1
    1. Advertising

  2. il Tue, 18 May 2004 17:15:26 -0400, "Richard Lionheart"
    <> 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 :)
     
    gabriele renzi, May 18, 2004
    #2
    1. Advertising

  3. 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
     
    Mark Sparshatt, May 18, 2004
    #3
  4. 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" <> wrote in message
    news:...
    > 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
     
    Richard Lionheart, May 18, 2004
    #4
  5. Richard Lionheart

    Dave Burt Guest

    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" <> 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>
     
    Dave Burt, May 19, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jmueto
    Replies:
    0
    Views:
    758
    jmueto
    Dec 17, 2004
  2. non-virtual thunk

    , Jul 3, 2007, in forum: C++
    Replies:
    2
    Views:
    960
  3. Replies:
    1
    Views:
    382
    Neelesh Bodas
    Jul 3, 2007
  4. Obnoxious User

    non-virtual thunk is?

    Obnoxious User, Jul 3, 2007, in forum: C++
    Replies:
    5
    Views:
    5,567
  5. bdb112
    Replies:
    45
    Views:
    1,346
    jazbees
    Apr 29, 2009
Loading...

Share This Page