From: akraynov Date: 2010-04-16T16:00:07+09:00 Subject: Re: Ruby-warrior : Teaching AI concepts with Ruby 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". 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?