Created
February 14, 2017 20:26
-
-
Save croz1007/0c1a682c63d36087652770ede14c0858 to your computer and use it in GitHub Desktop.
Flatten array without Ruby#flatten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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