Skip to content

Instantly share code, notes, and snippets.

@ijunaid8989
Created January 31, 2017 12:31
Show Gist options
  • Save ijunaid8989/461ca1ecf5daf4aa6499f13319ca6c27 to your computer and use it in GitHub Desktop.
Save ijunaid8989/461ca1ecf5daf4aa6499f13319ca6c27 to your computer and use it in GitHub Desktop.
#Easy one and available on stackoverflow
class Array
def flattify
each_with_object([]) do |element, flattened|
flattened.push *(element.is_a?(Array) ? element.flattify : element)
end
end
end
[1,2,3,4,[1,2,3,4],5].flattify # => [1, 2, 3, 4, 1, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment