From: Dustin Barker Date: 2008-07-21T07:29:03+09:00 Subject: Re: [QUIZ] Records and Arrays (#170) Here's my solution - playing w/ anonymous classes for the adapter solutions, probably not ideal since it doesn't provide all the methods a proper Array or arbitrary record would provide, but was fun to try out ;-) require 'ostruct' def extract_attributes(rec) if rec.kind_of?(OpenStruct) attributes = rec.marshal_dump.keys else attributes = rec.public_methods attributes.delete_if do |a| !(a =~ /^[a-z_]*$/i && attributes.include?("#{a}=")) end end attributes end def aor_to_roa(arr) attributes = extract_attributes(arr.first) roa = arr.first.class.new attributes.each { |a| roa.send("#{a}=", []) } arr.each do |rec| attributes.each do |a| roa.send(a).push(rec.send(a)) end end roa end def roa_to_aor(roa) attributes = extract_attributes(roa) arr = [] roa.send(attributes.first).size.times do |i| arr[i] = roa.class.new attributes.each do |a| arr[i].send("#{a}=", roa.send(a)[i]) end end arr end class ROAAdapter def initialize(arr) @arr = arr end def method_missing(a) arr = @arr Class.new do define_method(:[]) { |i| arr[i].send(a) } end.new end end class AORAdapter < Array def initialize(roa) @roa = roa end def [](i) roa = @roa Class.new do define_method(:method_missing) { |a| roa.send(a)[i] } end.new end end -Dustin