From: Michael Neumann Date: 2001-07-20T05:12:13+09:00 Subject: [ruby-talk:18149] Re: instance variables by name? jared l. jennings wrote: > On Fri, Jul 20, 2001 at 03:18:31AM +0900, Barnett, Aaron wrote: > > > > i'd like to have a method that operates on an instance variable given its > > name... > > > > class cat > > def initialize() > > @says = 'meow' > > end > > > > def set(name, value) > > @<> = value > > end > > end > > > > cat.new().set('says', 'arf') > > > > i figure it can be done but i cant find out how based on the documentation > > i've found. > > > > thanks > > aaron > > > > here ya go > > class Cat > def initialize > @says = 'meow' > end > > attr_accessor :says > > def set(name, value) > self.send(name+"=", value) Or without defining an attr_accessor before: eval("@#{name}=value") if name =~ /^\w+$/ Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de