Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

very newbie question

0 views
Skip to first unread message

duxieweb

unread,
Nov 15, 2009, 3:52:02 AM11/15/09
to
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.

Stefano Crocco

unread,
Nov 15, 2009, 3:57:34 AM11/15/09
to

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

duxieweb

unread,
Nov 15, 2009, 4:04:32 AM11/15/09
to
2009/11/15 Stefano Crocco <stefano...@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.

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.

7stud --

unread,
Nov 15, 2009, 8:44:05 AM11/15/09
to
duxieweb wrote:
> 2009/11/15 Stefano Crocco <stefano...@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.
>
> 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
$


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

7stud --

unread,
Nov 15, 2009, 9:07:28 AM11/15/09
to
duxieweb wrote:
> 2009/11/15 Stefano Crocco <stefano...@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.

Gennady Bystritsky

unread,
Nov 15, 2009, 10:51:11 AM11/15/09
to

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

> 2009/11/15 Stefano Crocco <stefano...@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.

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

>
> python:
>>>> x=0
>>>> if (not x):

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

Marnen Laibow-Koser

unread,
Nov 15, 2009, 11:00:16 AM11/15/09
to
duxieweb wrote:
> 2009/11/15 Stefano Crocco <stefano...@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.
>
> 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,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

Seebs

unread,
Nov 15, 2009, 1:28:03 PM11/15/09
to
On 2009-11-15, 7stud -- <bbxx78...@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.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Robert Klemme

unread,
Nov 15, 2009, 4:44:39 PM11/15/09
to
On 15.11.2009 19:28, Seebs wrote:
> On 2009-11-15, 7stud -- <bbxx78...@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

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

Steve Wilhelm

unread,
Nov 15, 2009, 9:26:05 PM11/15/09
to

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.

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

0 new messages