From: Wayne Brisette Date: 2013-07-10T19:02:58+09:00 Subject: Why can't I just subtract in a loop without restating the variable? ---1392739086-2068499309-1373450201=:77813 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable I have some code that uses a counter. In certain circumstances, I have to s= ubtract from the counter variable. I thought I could simple do this:=A0=0A= =0Amy_variable -1=A0=0A=0Aand my_variable would simply get reduced by one (= the same way we use the =3D+1)=0A=0AHowever, this is what I see:=A0=0A=0A1.= 9.3-p392 :001 > x =3D true=0A=A0=3D> true=A0=0A1.9.3-p392 :002 > y =3D fals= e=0A=A0=3D> false=A0=0A1.9.3-p392 :003 > @headcount =3D 4=0A=A0=3D> 4=A0=0A= 1.9.3-p392 :004 > if x=A0=0A1.9.3-p392 :005?> =A0 @headcount -3=0A1.9.3-p39= 2 :006?> =A0 elsif y=0A1.9.3-p392 :007?> =A0 @headcount -1=0A1.9.3-p392 :00= 8?> =A0 end=0A=A0=3D> 1=A0=0A1.9.3-p392 :009 > puts @headcount=0A4=0A=A0=3D= > nil=A0=0A=0A=0AIf I use @headcount =3D @headcount -1 the subtraction work= s. But I didn't think I had to do this... am I confused about that point?= =A0=0A=0AWayne ---1392739086-2068499309-1373450201=:77813 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable
I have some code that use= s 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 reduc= ed by one (the same way we use the =3D+1)

However,= this is what I see: 

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

<= div>
If I use @headcount =3D @headcount -1 the subtraction wo= rks. But I didn't think I had to do this... am I confused about that point? 

Wayne
---1392739086-2068499309-1373450201=:77813--