Skip to content

Instantly share code, notes, and snippets.

@kvokka
Created April 8, 2018 08:25
Show Gist options
  • Save kvokka/533b1a3462c6a4e6bf45bedb7cfc89cb to your computer and use it in GitHub Desktop.
Save kvokka/533b1a3462c6a4e6bf45bedb7cfc89cb to your computer and use it in GitHub Desktop.
own flatten
def my_flatten(s)
s.inject([]) do |arr, el|
el.respond_to?(:to_ary) ? my_flatten(arr+el) : arr.push(el)
end
end
samples = [
[[[2,3],[5,6,7],[[8,9],10],11,12]],
[1,2,[3],4],
[:foo, 'bar', 42, [{baz: 23}]]
]
samples.all?{ |s| my_flatten(s) == s.flatten } || raise
puts 'Done ;-)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment