Skip to content

Instantly share code, notes, and snippets.

@chevinbrown
Created August 2, 2016 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chevinbrown/bfbaf56ce2b4f7cc557c13648733cc13 to your computer and use it in GitHub Desktop.
Save chevinbrown/bfbaf56ce2b4f7cc557c13648733cc13 to your computer and use it in GitHub Desktop.
Flatten Array of Integers
class Array
# Flattens only an array of integers with varying dimensions
def flatty!
self.to_s.tr('[]','').split(',').map(&:to_i)
end
def flatty_test
a1 = [[1, 2, [3]], 4]
a2 = [1, [2, 3, 4], [[[5, [6]]]]]
puts a1.flatty!.to_s
puts a2.flatty!.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment