I've got a class, like this one:
class A
attr_accessor(:field2)
attr_accessor(:field1)
end
What's the best way to produce a Hash out of it with keys and values taken from the class instance?
And what's the best way to populate the instance of class A with values from that Hash?
=====
I'm probably looking for something similar to JavaBeans introspection that would give me the names of the data object fields, then execute logic based on this info. Ruby is a very modern flexible and dynamic language and I refuse to admit that it will not let me do things that I can easily do with Java ;-)
=====
In the end I found out that Struct is the best option:
a = {:a => 'qwe', :b => 'asd'}
s = Struct.new(*a.keys).new(*a.values) # Struct from Hash
h = Hash[*s.members.zip(s.values).flatten] # Hash from Struct