From: "David A. Black" Date: 2004-04-24T01:14:07+09:00 Subject: Re: attr_accessor vs. local variable Hi -- On Sat, 24 Apr 2004, Its Me wrote: > Ruby not seem to predictably recognize attribute accessors (including > setters) as such unless preceded by "self.attr". But it recognizes other > self messages fine. I suspect this has something to do with its rule on what > is deemed a local variable. That's correct, except that there's nothing unpredictable about it, and it's not specific to methods defined with the attr_* family. > What rule / ambiguity causes this? > > class A > attr_accessor :x, :y > > def initialize > p x # local or @x? Strictly speaking, neither; in your example, it's a call to the method x (which you happen to have defined as , but could have defined in many different ways). > x=5 # does not call self.x= If it looks like a local variable assignment, then Ruby will define a local variable of that name at compile time, and will execute the assignment at runtime. Since all bare "var = value" expressions get claimed by this process, the way to insulate calls to '=' methods is to make the receiver explicit: > self.y=5 # sets @y which means it can no longer be interpreted as a variable assignment. David -- David A. Black dblack@wobblini.net