[Nitro] Help creating a ObjectGraph adapter for RubyAMF

Jonathan Buch john at oxyliquit.de
Thu May 24 02:07:23 EDT 2007


Hi,

> Is there an easier way of doing this:
> result.inject([]) do |array,some_model|
>   array << column_names.inject({}) do |hash,col|
>     hash[col] = some_model.send(col)
>     hash
>   end
> end

> Just seems like a lot of work. Also If I do have to do it that way,
> where does the some_model variable come from?

Ah, no, of course you don't.  :)  Use any Ruby means of iterating
over 2 lists (result which has Og objects, column_names which has
symbols which lead to methods on the models).

I get from your answer, that you're not yet fully comfortable with
Ruby as of yet.  Read up the standard documentation for `.inject`.
In fact .inject shouldn't really be used, as it's slow and can
lead to misunderstandings.  It just flows from my fingers.  :P

rows = []
result.each do |some_model|
   row = []
   column_names.each do |col|
     row << some_model.send(col)
   end
   rows << row
end

This is probably more readable to you.  Your AR part does the same
in a slightly uglier way (.upto(.length) is unnecessary here).

Hope that helps,

Jo

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


More information about the Nitro-general mailing list