Ruby | Array class flatten!() function
Last Updated :
08 Jan, 2020
flatten!() is a Array class method which returns the flattened the array and returns nil if there is no modification required
Syntax: Array.flatten!()
Parameter: Array
Return: flattened array or nil if no modification is required
Example #1 :
a = [[ 18 , 22 ],
[ 33 , nil , 5 , 6 ]]
b = [[[ 1 , 4 , 1 , 1 , 88 , 9 ]]]
c = [ 18 , 22 , nil , nil , 50 , 6 ]
puts "flatten! : #{a.flatten!()}\n\n"
puts "flatten! : #{b.flatten!()}\n\n"
puts "flatten! : #{c.flatten!()}\n\n"
|
Output :
flatten! : [18, 22, 33, nil, 5, 6]
flatten! : [1, 4, 1, 1, 88, 9]
flatten! :
Example #2 :
a = [[ "abc" , "nil" , ],
[ "dog" ]]
b = [[[[ "cow" , nil , "dog" ]]]]
c = [ "cat" , nil , nil ]
puts "flatten! : #{a.flatten!()}\n\n"
puts "flatten! : #{b.flatten!()}\n\n"
puts "flatten! : #{c.flatten!()}\n\n"
|
Output :
flatten! : ["abc", "nil", "dog"]
flatten! : ["cow", nil, "dog"]
flatten! :
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...