flatten() is an Array class method which returns flattened array i.e. a 1D array
Syntax: Array.flatten()
Parameter: Array
Return: 1D array
Example #1 :
a = [[ 18 , 22 ],
[ 33 , nil , 5 , 6 ]]
b = [[[ 1 , 4 , 1 , 1 , 88 , 9 ]]]
puts "flatten : #{a.flatten()}\n\n"
puts "flatten : #{b.flatten()}\n\n"
|
Output :
flatten : [18, 22, 33, nil, 5, 6]
flatten : [1, 4, 1, 1, 88, 9]
Example #2 :
a = [[ "abc" , "nil" , ],
[ "dog" ]]
b = [[[[ "cow" , nil , "dog" ]]]]
puts "flatten : #{a.flatten()}\n\n"
puts "flatten : #{b.flatten()}\n\n"
|
Output :
flatten : ["abc", "nil", "dog"]
flatten : ["cow", nil, "dog"]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
08 Jan, 2020
Like Article
Save Article