J
Jesús Gabriel y Galán
Hello guys! I consider RubyWarrior may be useful for learning. A have
reached for 6'th level (as beginner).
Now my code looks as following: http://good.net/_5pJ5Rurvz .
I can't understand issue: if i call in method:
=A0 =A0def roam_to_combat!
=A0 =A0 =A0debug("Entering roam_to_combat")
=A0 =A0 =A0state =3D :ws_combat
=A0 =A0 =A0@ini_health =3D health
=A0 =A0 =A0debug(@ini_health)
=A0 =A0 =A0warrior.attack!(cur_dir)
=A0 =A0end
then it lacks to execute string "state =3D :ws_combat".
But if i call
=A0 =A0def roam_to_combat!
=A0 =A0 =A0debug("Entering roam_to_combat")
=A0 =A0 =A0self.state =3D :ws_combat # <-- here is difference
=A0 =A0 =A0@ini_health =3D health
=A0 =A0 =A0debug(@ini_health)
=A0 =A0 =A0warrior.attack!(cur_dir)
=A0 =A0end
then it executes "state =3D :ws_combat" normally.
Why it didn't execute "state =3D" without "self"?
Is it Ruby's bug?
I got Ruby 1.8 installed.
state =3D :ws_combat
is an assignment to a local variable. You are not using the variable
in the method, so i assume this is not what you want. When you do
self.state =3D :ws_combat
you are calling the method state=3D on the object, which I suppose you
have (maybe created by attr_accessor).
The fact that you have to call it this way is to disambiguate between
a local variable assignment and a method call with implicit receiver.
This is a consequence of the syntactic sugar that allows you have
methods name xxx=3D that look like assignments.
Jesus.