I have an array of hashes, which I would rather be turned into an array of objects with attributes programmatically added to those objects.
I’m try this at the moment
obj = OpenStruct.new
resulthash["users"].collect { |u|
u.each do |k,v|
obj.send("#{k}=#{v}");
end
}
To recap I’m trying to do
obj.foo = "bar"
obj.hello = "world"
But programmatically if for example to the array/hash looked liked this
{"users"=>[{"foo"=>"bar","hello"=>"world"}]}
Object#sendtakes a method name as the first argument and optionally arguments to pass to the method as the remaining arguments.Thus,
obj.send("#{k}=#{v}")really tries to call methods named something like"foo=bar", which is not the same as callingfoo=with an argument"bar".So for one, the right way would be
Note that I am using
#eachand not#collectbecause we do not want to transform the Hash.Also, if your example reflects your final goal of just converting an array of Hashes into a single OpenStruct, you can just combine all Hashes into one and pass that to
OpenStruct.new: