On Jul 18, 2008, at 11:03 AM, Matthew Moss wrote:

> ## Records and Arrays (#170)

Neat idea.  I was working with jQuery a lot last week and thinking  
along similar lines.

> 2. As an alternative to these two functions, create an adapter class
> that can wrap around "array of records" data to provide a "record of
> arrays" interface without actually moving data about.

I chose to run with this idea:

class MapToEnum
   instance_methods.each { |meth| undef_method(meth) unless meth =~ / 
\A__/ }

   def initialize(enum)
     @enum = enum
   end

   def __enum__
     @enum
   end

   def method_missing(meth, *args, &block)
     @enum.map { |o| o.send(meth, *args, &block) }
   end
end

def aor_to_roa(arr)
   MapToEnum.new(arr)
end

def roa_to_aor(mte)
   mte.__enum__
end

if __FILE__ == $PROGRAM_NAME
   Person = Struct.new(:name, :age)
   people = [ Person.new("James", 32),
              Person.new("Dana", 33),
              Person.new("Matthew", 36) ]

   wrapped = aor_to_roa(people)
   p wrapped.name
   p wrapped.name[2]

   p roa_to_aor(wrapped)
end

__END__

James Edward Gray II