Skip to content

Instantly share code, notes, and snippets.

@croz1007
Created February 14, 2017 20:26
Show Gist options
  • Save croz1007/0c1a682c63d36087652770ede14c0858 to your computer and use it in GitHub Desktop.
Save croz1007/0c1a682c63d36087652770ede14c0858 to your computer and use it in GitHub Desktop.
Flatten array without Ruby#flatten
def my_flat(a)
arr = []
if a.instance_of? (Array)
a.each do |b|
if b.instance_of? (Array)
arr += b.map {|c| c }
else
arr << b
end
end
else
arr << a
end
puts arr
end
arr1 = [1,2,3]
arr2 = [[4,5],6]
a = [arr1, arr2, 7, [8,9],10 ,11, 12, [13, 14, 15, 16]]
my_flat(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment