Skip to content

Instantly share code, notes, and snippets.

@mustafaturan
Created March 18, 2017 00:51
Show Gist options
  • Save mustafaturan/478393d132d811fb52e50c76ba8ee5c5 to your computer and use it in GitHub Desktop.
Save mustafaturan/478393d132d811fb52e50c76ba8ee5c5 to your computer and use it in GitHub Desktop.
def flatten(list, root = [])
list.each do |item|
item.is_a?(Array) ? flatten(item, root) : root.push(item)
end
root
end
test ".flatten" do
list = [[1,2,[3]],4, [9], 11]
assert_equal list.flatten, flatten(list)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment