Answer the question
In order to leave comments, you need to log in
How to beautifully write assignment of an array value to an object in Ruby?
I'm just a beginner rubist
obj.fio = data_after["fio"]
obj.phone = data_after["phone"]
obj.email = data_after["email"]
obj.address= data_after["address"]
obj.skype = data_after["skype"]
...
Answer the question
In order to leave comments, you need to log in
class MyClass
attr_accessor :name, :surname, :email
end
obj = MyClass.new
hash = {name: 'Ivan', surname: 'Ivanov', email: '[email protected]'}
hash.each do |key, value|
obj.public_send(%Q{#{key}=}, value) if obj.respond_to?(%Q{#{key}=})
end
p obj # => #<MyClass:0x007f9a4c11c478 @name="Ivan", @surname="Ivanov", @email="[email protected]">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question