Skip to content

Instantly share code, notes, and snippets.

@lmarburger
Forked from avdi/flatten.rb
Created December 16, 2009 02:50
Show Gist options
  • Save lmarburger/257550 to your computer and use it in GitHub Desktop.
Save lmarburger/257550 to your computer and use it in GitHub Desktop.
# Single-level flatten
arr = [ [1], [2,3], [4, [5]] ]
# Too flat!
arr.flatten # => [1, 2, 3, 4, 5]
# Just right!
arr.inject([]){|a,e| a.concat(e)} # => [1, 2, 3, 4, [5]]
# Just as right!
arr.flatten(1) # => [1, 2, 3, 4, [5]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment