📜  红宝石 |数组 clear() 操作

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

红宝石 |数组 clear() 操作

Array#clear() : clear()是一个 Array 类方法,它从中删除所有数组元素。

Syntax:  Array.clear()

Parameter:  Arrays to clear off

Return:  array with all elements cleared

代码 #1:clear() 方法的示例

# Ruby code for clear() method
   
# declaring array
a = [1, 2, 3, 4]
   
# declaring array
b = [111.11, 2.5, 4.3, 2.224]
  
# clearing the arrays
puts "clear a : #{a.clear()}\n\n"
  
puts "clear b : #{b.clear()}\n\n"

输出 :

clear a : []

clear b : []

代码 #2:clear() 方法的示例

# Ruby code for clear() method
   
# declaring array
a = ["abc", "xyz", "dog"]
   
# declaring array
b = ["cow", "cat", "dog"]
   
# declaring array
c = ["cat", "1", "dog"]
  
# clearing the array
puts "clear a : #{a.clear()}\n\n"
  
puts "clear b : #{b.clear()}\n\n"
  
puts "clear c : #{c.clear()}\n\n"

输出 :

clear a : []

clear b : []

clear c : []