From: "Jesús Gabriel y Galán" Date: 2012-08-03T22:25:53+09:00 Subject: Re: learning by doing part 2 - tc game On Fri, Aug 3, 2012 at 3:06 PM, Sebastjan H. wrote: > I wanted to test this on a simple case, so I wrote the following: > ------------------------------------------ > def attack(target) > target -= 200 > end > > class A > def initialize(name, hp) > @name = name > @hp = hp > end > > def hp() > @hp > end > > def name() > @name > end > > end > > players = [] << A.new("test", 400) > > attack(players[0].hp) > > puts players[0].hp > ------------------------------------------ > The code runs, but the result is 400. As if the attack method did > nothing. > What am I doing wrong? The reason is that this line: target -= 200 is equivalent to this one: target = target - 200 which is assigning a new value to the local variable of the method. Once you exit the method, the new value is lost. In order for this to work, you should pass a reference to a mutable object, so that the effects of the method are visible outside. For example: def attack(target) target.hp -= 200 # supposing target has methods #hp and #hp= end # later.... attack(player[0]) Hope this helps, Jesus.