Very newbie question

Hello,

I run these in irb:

irb(main):001:0> queue=0
=> 0
irb(main):002:0> if !queue then print "The queue is empty." end
=> nil

why the result is nil?
I thought it should be printing "The queue is empty."

Thanks.

Because 0 is a true value in ruby, so !queue is false and the body of the if
expression is not executed. In ruby, the only false values are false and nil.

I hope this helps

Stefano

···

On Sunday 15 November 2009, duxieweb wrote:

>Hello,
>
>I run these in irb:
>
>irb(main):001:0> queue=0
>=> 0
>irb(main):002:0> if !queue then print "The queue is empty." end
>=> nil
>
>why the result is nil?
>I thought it should be printing "The queue is empty."
>
>Thanks.
>

Because 0 is a true value in ruby, so !queue is false and the body of the if
expression is not executed. In ruby, the only false values are false and nil.

Oh, that sounds so different from other languages that 0 is a true value.

python:

x=0
if (not x):

... print "x is false"
...
x is false

perl:
# perl -le '$x=0; print "x is false" if not $x'
x is false

Thank you.

···

2009/11/15 Stefano Crocco <stefano.crocco@alice.it>:

duxieweb wrote:

Because 0 is a true value in ruby, so !queue is false and the body of the if
expression is not executed. In ruby, the only false values are false and nil.

Oh, that sounds so different from other languages that 0 is a true
value.

python:

x=0
if (not x):

... print "x is false"
...
x is false

perl:
# perl -le '$x=0; print "x is false" if not $x'
x is false

Thank you.

You might want to take a look at these examples:

python:
x = 0

if x:
    print "x is true"
else:
    print "x is false"

--output:--
x is false

perl:
$ perl -lwe 'my $x=0; if ($x) {print "x is true";} else {print "x is
false"}'
x is false
$

···

2009/11/15 Stefano Crocco <stefano.crocco@alice.it>:

--
Posted via http://www.ruby-forum.com/\.

duxieweb wrote:

···

2009/11/15 Stefano Crocco <stefano.crocco@alice.it>:

Because 0 is a true value in ruby, so !queue is false and the body of the if
expression is not executed. In ruby, the only false values are false and nil.

Oh, that sounds so different from other languages that 0 is a true
value.

Whoops. I misinterpreted what your issue was. Yes, ruby is strange
that way. In ruby, only nil evaluates to false (and of course false IS
false). As a result, everything but nil and false evaluates to
true--including 0.
--
Posted via http://www.ruby-forum.com/\.

Because 0 is a true value in ruby, so !queue is false and the body of the if
expression is not executed. In ruby, the only false values are false and nil.

Oh, that sounds so different from other languages that 0 is a true value.

Not completely true. In Java, for instance, it will produce a compile error like:

Test.java:40: operator ! cannot be applied to int
        if(!0) {
           ^
1 error

···

On Nov 15, 2009, at 1:04 AM, duxieweb wrote:

2009/11/15 Stefano Crocco <stefano.crocco@alice.it>:

python:

x=0
if (not x):

... print "x is false"
...
x is false

perl:
# perl -le '$x=0; print "x is false" if not $x'
x is false

Thank you.

duxieweb wrote:

Because 0 is a true value in ruby, so !queue is false and the body of the if
expression is not executed. In ruby, the only false values are false and nil.

Oh, that sounds so different from other languages that 0 is a true
value.

python:

x=0
if (not x):

... print "x is false"
...
x is false

perl:
# perl -le '$x=0; print "x is false" if not $x'
x is false

Perl has the additional complication that certain math operations will
return the string "0 but true", which is transparently converted to 0 in
a numeric context but still evaluates to true. So does 0.0, IIRC.

Thank you.

Best,

···

2009/11/15 Stefano Crocco <stefano.crocco@alice.it>:

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

duxieweb:

···

2009/11/15 Stefano Crocco <stefano.crocco@alice.it>:

Because 0 is a true value in ruby, so !queue is false and the body of
the if expression is not executed. In ruby, the only false values are
false and nil.

Oh, that sounds so different from
other languages that 0 is a true value.

That’s one of Ruby’s (more-or-less-)distinctive features, which
many Rubyists – myself included – actually find very elegant.

You might want to take a some of these links as
well: Ruby gotchas - Google Search

— Shot
--
X Window is the Iran-Contra of graphical user interfaces: a tragedy of
political compromises, entangled alliances, marketing hype, and just
plain greed. X Window is to memory as Ronald Reagan was to money.
                                            [The Unix Haters Handbook]

Lua's the same way.

I'm not comfortable with it yet, since I'm from a C background, but I
think it's logically preferable.

-s

···

On 2009-11-15, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

Whoops. I misinterpreted what your issue was. Yes, ruby is strange
that way. In ruby, only nil evaluates to false (and of course false IS
false). As a result, everything but nil and false evaluates to
true--including 0.

--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

I think initially I found it irritating, too. But that was just for a very short period. Nowadays I believe you hit the nail on the head: it's logically preferable. Often, code that looks up something in a Hash or other data structure will return nil if nothing is found. If you have numbers in there, code will get more complicated if also 0 can be stored , you want a 0 returned to be evaluated as a "hit" and the language would evaluate 0 as false in a boolean context.

Kind regards

  robert

···

On 15.11.2009 19:28, Seebs wrote:

On 2009-11-15, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

Whoops. I misinterpreted what your issue was. Yes, ruby is strange that way. In ruby, only nil evaluates to false (and of course false IS false). As a result, everything but nil and false evaluates to true--including 0.

Lua's the same way.

I'm not comfortable with it yet, since I'm from a C background, but I
think it's logically preferable.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

This as well as many other unique features of Ruby are covered very
clearly in 'The Ruby Programming Language' by Flanagan and Matsumoto. I
would recommend getting a copy and reading it.

- Steve W.

Robert Klemme wrote:

···

On 15.11.2009 19:28, Seebs wrote:

On 2009-11-15, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

Whoops. I misinterpreted what your issue was. Yes, ruby is strange
that way. In ruby, only nil evaluates to false (and of course false IS
false). As a result, everything but nil and false evaluates to
true--including 0.

Lua's the same way.

I'm not comfortable with it yet, since I'm from a C background, but I
think it's logically preferable.

I think initially I found it irritating, too. But that was just for a
very short period. Nowadays I believe you hit the nail on the head:
it's logically preferable. Often, code that looks up something in a
Hash or other data structure will return nil if nothing is found. If
you have numbers in there, code will get more complicated if also 0 can
be stored , you want a 0 returned to be evaluated as a "hit" and the
language would evaluate 0 as false in a boolean context.

Kind regards

  robert

--
Posted via http://www.ruby-forum.com/\.