Ruby-warrior : Teaching AI concepts with Ruby

  • Thread starter Aldric Giacomoni
  • Start date
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.
 
A

akraynov

state = :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 = :ws_combat

you are calling the method state= 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= that look like assignments.

Jesus.

Yes, "state" is assignment method. It behaves unsimiliar as other
languages:
"xxx=" can be declared in Object Pascal too, for example.
It seems local variable "state" has overriden my method "state".
This can be a source of many hidden bugs for those who well-skilled in
Delphi or C++.
Why Ruby doesn't give me any warning?
 
J

Jesús Gabriel y Galán

Yes, "state" is assignment method. It behaves unsimiliar as other
languages:
"xxx=3D" can be declared in Object Pascal too, for example.
It seems local variable "state" has overriden my method "state".

Yes, that's exactly what happens. The parser flags state as a local
variable, and not state=3D as a method call.
This can be a source of many hidden bugs for those who well-skilled in
Delphi or C++.
Why Ruby doesn't give me any warning?

I don't know.

Jesus.
 
A

Aldric Giacomoni

akraynov said:
It seems local variable "state" has overriden my method "state".
This can be a source of many hidden bugs for those who well-skilled in
Delphi or C++.
Why Ruby doesn't give me any warning?

Because it doesn't parse incorrectly; it just runs incorrectly. That's
why you need to do what Jesus explained.
The burden of intelligence is on the developer ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top