Skip to content

Instantly share code, notes, and snippets.

@orafaelfragoso
Created January 10, 2017 03:09
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 orafaelfragoso/799c3611458e6ca1960aecf045f2517e to your computer and use it in GitHub Desktop.
Save orafaelfragoso/799c3611458e6ca1960aecf045f2517e to your computer and use it in GitHub Desktop.
Flatten an Array without using Array.flatten()
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