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
>
> 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/.
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.
> 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"
> ...
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
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!
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 W.
--
Posted via http://www.ruby-forum.com/.