my solution (including a simple test)
require 'ostruct'
require 'test/unit'
# solution by rolando abarca - funkaster / gmail.com
module Quiz170
def self.aor_to_roa(arr)
os = OpenStruct.new
arr.each do |o|
# another way to get the keys:
# keys = (o.methods - OpenStruct.new.methods).delete_if { |m|
m.match(/=$/) }
o.instance_variable_get("@table").keys.each { |k|
(os.send(k).nil? ? os.send("#{k}=", []) : os.send(k)) <<
o.send(k)
}
end
os
end
def self.roa_to_aor(rec)
arr = []
table = rec.instance_variable_get("@table")
size = table[table.keys.first].size
0.upto(size-1) { |i|
obj = OpenStruct.new
table.keys.each { |k| obj.send("#{k}=", table[k][i]) }
arr << obj
}
arr
end
end
class TestQuiz170 < Test::Unit::TestCase
def setup
@data = []
1.upto(100) { |i|
_t = OpenStruct.new
_t.name = "t#{i}"
_t.data = (rand * 1000).round
@data << _t
}
@roa = Quiz170.aor_to_roa(@data)
end
def test_001
1.upto(100) {
idx = (rand * 100).to_i
assert_equal @data[idx].name, @roa.name[idx]
assert_equal @data[idx].data, @roa.data[idx]
}
end
def test002
assert_equal @data, Quiz170.roa_to_aor(@roa)
end
end
regards,
rolando./