On 7/18/08, Matthew Moss <matthew.moss / gmail.com> wrote: > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- > *Your task this week* is to define two functions that move data > between "array of records" storage and "record of arrays" storage. > You should make this work with [OpenStruct][1]; do not limit yourself > to the example records shown above. Here's my solution which works with OpenStruct only, I didn't do any extra credit. -Adam require 'ostruct' def aor_to_roa(arr) # This method accepts an array of records, and # returns a single record of arrays. rec = OpenStruct.new return rec if ! arr[0] #empty array => empty record vars = (arr[0].methods - rec.methods) setters = vars.select{|m|m[-1]==?=}.sort getters = (vars-setters).sort! vars = getters.zip setters vars.each{|get,set| rec.send(set, Array.new)} arr.each{|item| vars.each{|get,set| rec.send(get)<< item.send(get) } } rec end def roa_to_aor(rec) # This method accepts a record of arrays, and # returns a single array of records. arr=[] vars = (rec.methods - OpenStruct.new.methods) setters = vars.select{|m|m[-1]==?=}.sort getters = (vars-setters).sort! vars = getters.zip setters vars.each {|get,set| rec.send(get).each_with_index{|value,i| arr[i]||=OpenStruct.new arr[i].send(set,value) } } arr end