Ruby | Array class flatten!() function
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 :
# Ruby code for flatten!() method # declaring arraya = [[18, 22], [ 33, nil, 5, 6]] # declaring arrayb = [[[1, 4, 1, 1, 88, 9]]] # declaring arrayc = [18, 22, nil, nil, 50, 6] # flatten!puts "flatten! : #{a.flatten!()}\n\n" # flatten!puts "flatten! : #{b.flatten!()}\n\n" # flatten!puts "flatten! : #{c.flatten!()}\n\n" |
Output :
flatten! : [18, 22, 33, nil, 5, 6] flatten! : [1, 4, 1, 1, 88, 9] flatten! :
Example #2 :
# Ruby code for flatten!() method # declaring arraya = [["abc", "nil", ], ["dog"]] # declaring arrayb = [[[["cow", nil, "dog"]]]] # declaring arrayc = ["cat", nil, nil] # flatten!puts "flatten! : #{a.flatten!()}\n\n" # flatten!puts "flatten! : #{b.flatten!()}\n\n" # flatten!puts "flatten! : #{c.flatten!()}\n\n" |
Output :
flatten! : ["abc", "nil", "dog"] flatten! : ["cow", nil, "dog"] flatten! :


.png)