Created
January 10, 2017 03:09
-
-
Save orafaelfragoso/799c3611458e6ca1960aecf045f2517e to your computer and use it in GitHub Desktop.
Flatten an Array without using Array.flatten()
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
module ArrayHelper | |
def flatten(arr) | |
arr.each_with_object([]) do |el, flat| | |
flat.push *(el.is_a?(Array) ? self.flatten(el) : el) | |
end | |
end | |
" Creating a module function to the flatten method | |
So it can be called without being instantiated." | |
module_function :flatten | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment