Is this the way it should work? newbie problem


(MIcha) #1

Hi,

that seems not to work: (it’ ported from perl to ruby:-)

x = max_x if --x == 0;

the variable x is never decremented.
why?

thanks for helping
Michael


(Philip Mak) #2

I believe the above code is semantically equivalent to this:

if --x == 0
x = max_x
end

or this:

if x - 1 == 0
x = max_x
end

The statement “x = max_x” overwrites the effect that “–x” had.

···

On Tue, Jul 02, 2002 at 07:30:23AM +0900, MIcha wrote:

that seems not to work: (it’ ported from perl to ruby:-)

x = max_x if --x == 0;

the variable x is never decremented.
why?


(HAL 9000) #3

that seems not to work: (it’ ported from perl to ruby:-)

x = max_x if --x == 0;

the variable x is never decremented.
why?

The problem is that Ruby has no auto(inc|dec)rement
operators, either pre or post forms. (For reasons I’m
too lazy to go into here.)

I think what you’ve written actually means:

x = x_max if -(-x) == 0

You can say this in Ruby to do what you want:

x = x_max if (x-=1) == 0

HTH,
Hal Fulton

···

----- Original Message -----
From: “MIcha” mwohlwend@web.de
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, July 01, 2002 5:30 PM
Subject: is this the way it should work? newbie problem…


(Austin Ziegler) #4

Ruby doesn’t have ++ or --. For incrementing, the succ method is the
preferred way, but (at least according to Pickaxe) there is no
corresponding prev method. You can simulate it with the modifications
below. Note that I am still having to do assignment because Fixnum is
an immediate value (you can’t do self -= amt like you could in other
cases).

class Integer
def dec(amt = 1)
self - amt
end

def inc(amt = 1)
    self + amt
end

end

p 5.dec # => 4
p 5.dec(3) # => 3
p 5.inc # => 6
p 5.inc(3) # => 8

max_x = 50
x = 2

x = x.dec
x = max_x if (x = x.dec) == 0;
p x

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.01 at 18.32.35

···

On Tue, 2 Jul 2002 07:30:23 +0900, MIcha wrote:

Hi,

that seems not to work: (it’ ported from perl to ruby:-)

x = max_x if --x == 0;

the variable x is never decremented.
why?


(MIcha) #5

o.k., o.k.,
I should read more than just a few pages of the language definition :slight_smile:
you can laugh :slight_smile:

Michael

···

On Tue, 02 Jul 2002 00:09:28 +0200, MIcha wrote:

x = max_x if --x == 0;


(David Alan Black) #6

Hello –

···

On Tue, 2 Jul 2002, Philip Mak wrote:

On Tue, Jul 02, 2002 at 07:30:23AM +0900, MIcha wrote:

that seems not to work: (it’ ported from perl to ruby:-)

x = max_x if --x == 0;

the variable x is never decremented.
why?

I believe the above code is semantically equivalent to this:

if --x == 0
x = max_x
end

or this:

if x - 1 == 0
x = max_x
end

The statement “x = max_x” overwrites the effect that “–x” had.

Which is none :slight_smile:

Ruby doesn’t have – or ++ operators. --x means negative negative x
(in other words, x).

David


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