From: Jeremy Bopp Date: 2010-12-10T08:26:12+09:00 Subject: Re: is my instance variable being masked? On 12/09/2010 04:20 PM, Sylvester T Cat wrote: > This is probably trivial for the pros here, but > > I have a class > > class MyClass > attr_accessor :raw_vehicle_data > > def parse_raw_vehicle_data > @raw_vehicle_data = [0,1,2,3,4] > puts raw_vehicle_data.size # => gives proper size of the array > (i.e. size > 0) > # do other stuff with raw_vehicle_data > end > end > > irb(main):019:0> my_class = MyClass.new > => # > irb(main):020:0> my_class.parse_raw_vehicle_data > 5 #<= this value I expect, but it's not assigned to my "instance > variable" > => nil > irb(main):021:0> my_class.raw_vehicle_data.size > => nil > > If I change the line from raw_vehicle_data = [0,1,2,3,4] to > @raw_vehicle_data = [0,1,2,3,4], then everything works properly, but I > thought attr_accessor gave me access to the function > "raw_vehicle_data=" that would assign to an instance variable. Your code listing above has the @raw_vehicle_data reference, but I think you meant to leave off the @ there for your problem demonstration. Leaving off the @ introduces an ambiguity for Ruby in assignment statements like yours. The left hand side is seen as a local variable reference rather than a call to the attr_writer method of the same name. You can either use the @ as you discovered, or you can explicitly call the attr_writer: self.raw_vehicle_data = [0,1,2,3,4] -Jeremy