Hi, I''ve got a hash: {:colA=>["blue","red","green"], :colB=>["diesel","fuel cell","gas"], colC=>["bus","truck","car"]} All arrays are the same size. I would like to turn it into a array of line , and get this [ ["blue","diesel","bus"], ["red","fuel cell","truck"], ["green","gas","car"] ] What is the best way to do this? Thanks --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I''ve got a hash: > > {:colA=>["blue","red","green"], :colB=>["diesel","fuel cell","gas"], > colC=>["bus","truck","car"]} > > All arrays are the same size. > > I would like to turn it into a array of line , and get this > > [ > ["blue","diesel","bus"],["red","fuel cell","truck"],> ["green","gas","car"] > ] > > What is the best way to do this?No idea if it''s the best... irb> h = {:colA=>["blue","red","green"], :colB=>["diesel","fuel cell","gas"], :colC=>["bus","truck","car"]} irb> (0..2).map {|i| [h[:colA][i], h[:colB][i], h[:colC][i]]} => [["blue", "diesel", "bus"], ["red", "fuel cell", "truck"], ["green", "gas", "car"]] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, May 20, 2008 at 08:51:01AM -0700, jef wrote:> > Hi, > > I''ve got a hash: > > {:colA=>["blue","red","green"], :colB=>["diesel","fuel cell","gas"], > colC=>["bus","truck","car"]} > > All arrays are the same size. > > I would like to turn it into a array of line , and get this > > [ > ["blue","diesel","bus"], > ["red","fuel cell","truck"], > ["green","gas","car"] > ] > > What is the best way to do this?There are two pieces here. One is turning it into an arrays of arrays, the other is transposing. If the order matters, use this to get the array of arrays: arr = [:colA, :colB, :colC].map { |k| hash[k] } ...otherwise: arr = hash.values Once you have an array of arrays, there is already a transpose method: arr = arr.transpose> Thanks--Greg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---