Regardless of whether it’s good practice or not, how can I dynamically call accessor methods in Ruby?
Here’s an example class:
class Test_Class attr_accessor :a, :b end
I can use the Object.send method to read the variable…
instance.a = 'value' puts( instance.send( 'a' ) ) # => value
But I’m having a hard time trying to write to it. These throw ‘wrong number of arguments (1 for 0) (ArgumentError)’
instance.send('a', 'value')
and
instance.method('a').call('value')
Please help me StackOverflow!
I am not a ruby expert, but I think that you could do: