From: Nicholas Clare Date: 2007-05-10T04:34:29+09:00 Subject: Re: using dynamic method calling with attr_writer methods ------=_Part_63504_17838988.1178739267682 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 5/9/07, Markus Hohenhaus wrote: > > I'm a total Ruby newbie looking for help. > > I'm writing an application where I need to dynamical assign object > variables via attr_writer object methods. > > My code is something like this: > > class Test < ParentClass > attr_reader :one, :two, :three > attr_writer :one, :two, :three > > def initialize > super > end > > end > ( I know I could use attr_accessor but for testing I'll stick to > attr_writer and _reader) > > Now I try to call the 'Test' methods dynamical witch 'object.send' from > within my main application: > > [...] > > @methods = ['one', 'two', 'three'] > > test = Test.new > > @methods.each { |a| test.send(a, 'foo') } > > [...] > > This throws the error: ArgumentError: wrong number of Arguments (1 for > 0) > > As far as I understand, this is the correct behaviour. Because without > the second argument, the method call returns the value of the class > variable. So I believe, that this should work if i'll write all the > setter and getter methods myself (which I don't want to, because I'm > using a lot of variables). > Are there any other methods I can use to dynamically call attr_writer > methods to assign values to the class variables or did I get something > wrong and made a mistake? > > -- > Posted via http://www.ruby-forum.com/. > > I'm pretty new to ruby too, but from what I understand, the writer methods will be called one=, two=, etc So changing that @methods array to include the equals should fix your problem Hope this helps, Nick ------=_Part_63504_17838988.1178739267682--