Created
January 31, 2017 12:31
-
-
Save ijunaid8989/461ca1ecf5daf4aa6499f13319ca6c27 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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