Why can't I just subtract in a loop without restating the variable?

I have some code that uses a counter. In certain circumstances, I have
to subtract from the counter variable. I thought I could simple do this:

my_variable -1

and my_variable would simply get reduced by one (the same way we use the
=+1)

However, this is what I see:

1.9.3-p392 :001 > x = true
=> true
1.9.3-p392 :002 > y = false
=> false
1.9.3-p392 :003 > @headcount = 4
=> 4
1.9.3-p392 :004 > if x
1.9.3-p392 :005?> @headcount -3
1.9.3-p392 :006?> elsif y
1.9.3-p392 :007?> @headcount -1
1.9.3-p392 :008?> end
=> 1
1.9.3-p392 :009 > puts @headcount
4
=> nil

If I use @headcount = @headcount -1 the subtraction works. But I didn’t
think I had to do this… am I confused about that point?

Wayne

-1 is not the same syntax as +=1.
You want -=1

Am 10.07.2013 12:02, schrieb Wayne B.:

I have some code that uses a counter. In certain circumstances, I have
to subtract from the counter variable. I thought I could simple do this:

my_variable -1

This returns the reduced value, but `my_variable’ is not modified.

Use

my_var = my_var - 1

or

my_var -= 1

Regards,
Marcus

Thanks!

Wayne

you should use

@head_count -= 1

Regards,
Martin