Skip to content

Instantly share code, notes, and snippets.

@deeTEEcee
Last active February 20, 2017 18:12
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 deeTEEcee/b627324c277fe49468519e10f1cf84b7 to your computer and use it in GitHub Desktop.
Save deeTEEcee/b627324c277fe49468519e10f1cf84b7 to your computer and use it in GitHub Desktop.
Ruby internals
# an imitation of ruby's flatten which takes in a 2nd argument
def flatten(array, n=nil)
n = -1 if !n
while n != 0
is_flat = true
array = array.inject([]) do |acc, x|
if x.is_a?(Array)
is_flat = false
acc + x
else
acc + [x]
end
end
n -= 1 if n
return array if is_flat
end
return array
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment