On Fri, Aug 3, 2012 at 3:44 PM, Sebastjan H. <lists / ruby-forum.com> wrote:
> In this line you wrote
>
>>   target.hp -= 200 # supposing target has methods #hp and #hp=
>
>
> what is the meaning of the equal sign? #hp=

It's part of the method name. Ruby has a bit of sintactic sugar to
allow calling methods that end with = as if they were assignments:

target.hp = 200

is equivalent to: target.hp=(200).

> Because in my case above the target only has hp method.
>
> And if I remodel it to your example:
>
> def attack(target)
> target.hp -= 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])
> puts players[0].hp
> -------------------------------------
> 3.rb:3:in `attack': undefined method `hp=' for #<A:0x8b7d268
> @name="test", @hp=400> (NoMethodError)
>   from 3.rb:24:in `<main>'

If you want the external classes the possibility to read and assign
new values to hp and name, you can use attr_accessor:

class A
  attr_accessor :name, :hp
  def initialize(name, hp)
    @name = name
    @hp = hp
  end
end

attr_accessor will create methods name, name=, hp and hp= for you.

Jesus.