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.
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.
On Sunday 15 November 2009, duxieweb wrote:
|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
2009/11/15 Stefano C. [email protected]:
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:
x is false
Thank you.
duxieweb wrote:
2009/11/15 Stefano C. [email protected]:
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.
On Nov 15, 2009, at 1:04 AM, duxieweb wrote:
2009/11/15 Stefano C. [email protected]:
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
duxieweb wrote:
2009/11/15 Stefano C. [email protected]:
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 falseperl:
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
$
duxieweb wrote:
2009/11/15 Stefano C. [email protected]:
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 falseperl:
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.
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
On 2009-11-15, 7stud – [email protected] 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
duxieweb:
2009/11/15 Stefano C. [email protected]:
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
On 15.11.2009 19:28, Seebs wrote:
On 2009-11-15, 7stud – [email protected] 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
This as well as many other unique features of Ruby are covered very
clearly in ‘The Ruby P.ming Language’ by Flanagan and Matsumoto. I
would recommend getting a copy and reading it.
Robert K. wrote:
On 15.11.2009 19:28, Seebs wrote:
On 2009-11-15, 7stud – [email protected] 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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs