From: Austin Ziegler Date: 2005-10-26T03:28:34+09:00 Subject: Re: having reference problems On 10/25/05, shalperin wrote: > Here is a code snippet: > > class Foo > @a > @b > @reference This is not necessary and definitely does not do what you want it to do. It actually accesses the @a, @b, and @reference variables in the class Foo. > def initialize() > @reference = [@a, @b] > end > > attr_writer :a, :b, :reference > attr_reader :a, :b, :reference > end > > f = Foo.new() > f.reference[0] = 1 > puts f.a #nil WTF! > > What I am trying to do is refer to an instance variable both by its > index in some array, and also directly. > > Any help would be greatly apreciated. Hmmm. Won't work, because variables in Ruby are just labels, not shoeboxes as they are in other programming languages. What problem are you adctually trying to solve and we can help you find a Ruby way of doing it. class Foo def initialize @reference = [] end attr_accessor :a remove_method :a= ; def a=(_a) @reference[0] = @a = _a end attr_accessor :b remove_method :b= ; def b=(_b) @reference[1] = @b = _b end attr_reader :reference end That's closer to what you want, but it's very static and fragile. I recommned looking closer at what you're trying to solve and finding another solution. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca