📜  红宝石 |数组类 flatten!()函数

📅  最后修改于: 2022-05-13 01:55:03.381000             🧑  作者: Mango

红宝石 |数组类 flatten!()函数

flatten!()是一个 Array 类方法,它返回展平后的数组,如果不需要修改则返回 nil

示例 #1:

# Ruby code for flatten!() method
  
# declaring array
a = [[18, 22],
    [ 33, nil, 5, 6]]
  
# declaring array
b = [[[1, 4, 1, 1, 88, 9]]]
  
# declaring array
c = [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"    

输出 :

flatten! : [18, 22, 33, nil, 5, 6]

flatten! : [1, 4, 1, 1, 88, 9]

flatten! : 

示例 #2:

# Ruby code for flatten!() method
  
# declaring array
a = [["abc", "nil", ],
    ["dog"]]
  
# declaring array
b = [[[["cow", nil, "dog"]]]]
  
# declaring array
c = ["cat", nil, nil]
  
# flatten!
puts "flatten! : #{a.flatten!()}\n\n"
  
# flatten!
puts "flatten! : #{b.flatten!()}\n\n"
  
# flatten!
puts "flatten! : #{c.flatten!()}\n\n"    

输出 :

flatten! : ["abc", "nil", "dog"]

flatten! : ["cow", nil, "dog"]

flatten! :