I am newish to Ruby and I am trying to write a method to dynamically add methods to n existing ruby class, here is what I have so far:
class Person
end
def attr_addr (target, attr)
target.send :attr_accessor, attr
end
bob = Person.new
attr_addr(Person,"name")
bob.name = "bob"
But I get:
private method `name=' for ....
What am I doing wrong here? – am I using the wrong approach entirely ;-)?
Your original code works for me. Anyway, here’s another way to write it:
Here it is in action in an IRB session: