How can I perform an idiomatic non-recursive flatten in ruby?

Posted by nasmorn on Stack Overflow See other posts from Stack Overflow or by nasmorn
Published on 2010-06-09T09:13:25Z Indexed on 2010/06/09 9:32 UTC
Read the original article Hit count: 237

Filed under:

I have a method that returns an array of arrays. For convenience I use collect on a collection to gather them together.

arr = collection.collect {|item| item.get_array_of_arrays}

Now I would like to have a single array that contains all the arrays. Of course I can loop over the array and use the + operator to do that.

newarr = []    
arr.each {|item| newarr += item}

But this is kind of ugly, is there a better way?

© Stack Overflow or respective owner

Related posts about ruby