Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Flatten an array.
- #
- # @param input [Array] An arbitrarily nested array structure of integers to be nested
- # @return [Array] Flattened one-dimensional array of integers
- def flatten(input)
- return [input] unless input.respond_to?('each')
- output = []
- input.each do |elem|
- if elem.respond_to?('each')
- output.concat(flatten(elem))
- else
- output << elem
- end
- end
- output
- end
Add Comment
Please, Sign In to add comment