very newbie question

D

duxieweb

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.
 
S

Stefano Crocco

|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
 
D

duxieweb

2009/11/15 Stefano Crocco said:
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:... print "x is false"
...
x is false

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


Thank you.
 
7

7stud --

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

python:
... 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
$
 
7

7stud --

duxieweb said:
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.
 
G

Gennady Bystritsky

=20
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
 
M

Marnen Laibow-Koser

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

python:
... 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,
 
S

Seebs

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
 
R

Robert Klemme

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
 
S

Steve Wilhelm

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,110
Latest member
OdetteGabb
Top