I found this interesting answer :
https://stackoverflow.com/a/2348854/169277
This is ok when you're trying to set instance variables it works really great.
Is there a way to apply the same logic or better one to create generic constructor like :
def initialize(obj)
obj.each do |k,v|
#find the setter for each k and set the value v to and return newly created object
end
end
If I had object TestObject:
class TestObject
attr_accessor :name, :surname, :sex
end
I was thinking to create it something like this:
TestObject.new({:name => 'Joe', :surname => 'Satriani'})
How would one achieve this?
So doing this would be a shorthand of :
t = TestObject.new
t.name = 'Joe'
t.surname = 'Satriani'