Skip to content

Instantly share code, notes, and snippets.

@pocha
Created August 9, 2016 13:10
Show Gist options
  • Save pocha/61e2f05a1df91348a230508ac7023b97 to your computer and use it in GitHub Desktop.
Save pocha/61e2f05a1df91348a230508ac7023b97 to your computer and use it in GitHub Desktop.
def flatten(a)
out = []
if !a.kind_of?(Array)
puts "not an array"
return -1
end
a.each do |val|
out += _flatten(val)
end
out
end
def _flatten(val)
out = []
if val.kind_of?(Array)
val.each do |_val|
out += _flatten(_val)
end
else
out << val
end
out
end
#a = [1,2,[[3],4]]
a = "pocha"
puts flatten(a).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment