M
Markus Hohenhaus
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 ne, :two, :three
attr_writer ne, :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?
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 ne, :two, :three
attr_writer ne, :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?