From: "Jesús Gabriel y Galán" Date: 2010-04-16T16:42:12+09:00 Subject: Re: Ruby-warrior : Teaching AI concepts with Ruby On Fri, Apr 16, 2010 at 9:00 AM, akraynov wrote: > On 16 апр, 03:25, Jesús Gabriel y Galán > wrote: >> On Thu, Apr 15, 2010 at 10:00 PM, akraynov wrote: >> > 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: >> >    def roam_to_combat! >> >      debug("Entering roam_to_combat") >> >      state = :ws_combat >> >      @ini_health = health >> >      debug(@ini_health) >> >      warrior.attack!(cur_dir) >> >    end >> >> > then it lacks to execute string "state = :ws_combat". >> > But if i call >> >    def roam_to_combat! >> >      debug("Entering roam_to_combat") >> >      self.state = :ws_combat # <-- here is difference >> >      @ini_health = health >> >      debug(@ini_health) >> >      warrior.attack!(cur_dir) >> >    end >> > then it executes "state = :ws_combat" normally. >> >> > Why it didn't execute "state =" without "self"? >> > Is it Ruby's bug? >> > I got Ruby 1.8 installed. >> >> 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". Yes, that's exactly what happens. The parser flags state as a local variable, and not state= 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.