& operation and if statment

I’m puzzled by the behaviour in the following run. I would expect line 2
to not print out “ouch” because the result of the if statement is 0
(verified online 8)

$ ruby -v
ruby 1.7.3 (2002-09-20) [i386-cygwin]

$ irb
irb(main):001:0> data = 0x00
0
irb(main):002:0> puts “ouch” if (data & 0x80)
ouch
nil
irb(main):003:0> puts “ouch” if (data & 0x80) != 0
nil
irb(main):004:0> data = 0x90
144
irb(main):005:0> puts “ouch” if (data & 0x80)
ouch
nil
irb(main):006:0> puts “ouch” if (data & 0x80) != 0
ouch
nil
irb(main):007:0> data = 0x00
0
irb(main):008:0> data & 0x80
0
irb(main):009:0>

Rob

-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
http://www.newsfeed.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----

Hi –

···

On Tue, 15 Oct 2002, Robert McGovern wrote:

I’m puzzled by the behaviour in the following run. I would expect line 2
to not print out “ouch” because the result of the if statement is 0
(verified online 8)

$ ruby -v
ruby 1.7.3 (2002-09-20) [i386-cygwin]

$ irb
irb(main):001:0> data = 0x00
0
irb(main):002:0> puts “ouch” if (data & 0x80)
ouch

Don’t forget that 0 is true.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

It is Item 12 in “Things That Newcomers to Ruby Should
Know” (http://www.glue.umd.edu/~billtj/ruby.html) :slight_smile:

Regards,

Bill

···

============================================================================
dblack@candle.superlink.net wrote:

On Tue, 15 Oct 2002, Robert McGovern wrote:

I’m puzzled by the behaviour in the following run. I would expect line 2
to not print out “ouch” because the result of the if statement is 0
(verified online 8)
$ irb
irb(main):001:0> data = 0x00
0
irb(main):002:0> puts “ouch” if (data & 0x80)
ouch

Don’t forget that 0 is true.

$ irb
irb(main):001:0> data = 0x00
0
irb(main):002:0> puts “ouch” if (data & 0x80)
ouch

Don’t forget that 0 is true.

Duh, thank you, I had compelely forgotten that.

I still find myself easily slipping into “other language mode”.

Rob

-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
http://www.newsfeed.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----

Don’t forget that 0 is true.

Duh, thank you, I had compelely forgotten that.

Er… Not that I couldn’t fix my scripts (my server monitor
just stopped alerting me of a recent downtime exactly for
this mistake…), but, Matz, is “if 0” being the same as
“if true” your final word on this? :wink:

Cheers,
Sab

Better to say “everything is true except nil and false” rather than
“everything is true except nil, false, zero (integer, not float), and
the empty string, except when the objects happen to be hidden behind a
proxy or alternative implementation”. No?

···
  • Szabolcs Szasz (sz@szasz.hu) wrote:

Er… Not that I couldn’t fix my scripts (my server monitor just
stopped alerting me of a recent downtime exactly for this mistake…),
but, Matz, is “if 0” being the same as “if true” your final word on
this? :wink:


Thomas ‘Freaky’ Hurst - freaky@aagh.net - http://www.aagh.net/

Everbody wants a pain shot at the same time.

Hello Szabolcs,

Tuesday, October 15, 2002, 1:43:53 AM, you wrote:

this mistake…), but, Matz, is “if 0” being the same as
“if true” your final word on this? :wink:

see str.index, for example. this is already used in language design
heavily, so changing this rule will brea compatibility in a serious
way

···


Best regards,
Bulat mailto:bulatz@integ.ru

One of the things I find annoying about Perl is having to write everywhere
    if (defined $x)
or if (exists $x{$y})

because 'if ($x)' introduces subtle special-case bugs (where $x is a string
containing exactly "0", for example) - and I see many examples of code which
is broken in this way. IMO those surprises, in 0.01% of cases, are far more
annoying and difficult to track down than something which persistently fails
simply because the language does not do what a 'C' programmer would expect.

Also, bitwise tests don't occur very frequently. I'd be quite happy to write
    puts "yippee" if (data & 0x80) == 0
and it makes the intention very clear.

Perhaps it would be helpful if ruby -w generated a warning if a Fixnum is
used as a truth value; that would force you to write
    if myint != 0
(However, if you don't know the type of an object in advance you could
also be forced to write 'unless param.nil?' instead of 'if param' to
avoid this warning, which takes us back to the Perl situation)

Regards,

Brian.

···

On Tue, Oct 15, 2002 at 06:43:53AM +0900, Szabolcs Szasz wrote:

> > Don't forget that 0 is true.
>
> Duh, thank you, I had compelely forgotten that.

Er... Not that I couldn't fix my scripts (my server monitor
just stopped alerting me of a recent downtime exactly for
this mistake...), but, Matz, is "if 0" being the same as
"if true" your final word on this? :wink: