📜  红宝石 |清除()函数

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

红宝石 |清除()函数

Ruby 中的 clear()函数用于删除给定数组的所有元素并返回没有元素的数组。

示例 1:

# Initializing some arrays of elements
Array1 = ["Alphabets", "a", "b", "c", "d", "e"]
Array2 = ["Names", "gfg", "Geeks", "Geek", "GeeksforGeeks"]
Array3 = ["City", "Kolkata", "Mumbai", "Delhi", "Patna"]
  
# Calling clear() function
A = Array1.clear
B = Array2.clear
C = Array3.clear
  
# Printing the cleared array
puts "#{A}"
puts "#{B}"
puts "#{C}"

输出:

[]
[]
[]

示例 2:

# Initializing some arrays of elements
Array1 = []
Array2 = [1, 2]
Array3 = ["a", "b", "c"]
  
# Calling clear() function and
# Printing the cleared array
puts "#{Array1.clear}"
puts "#{Array1.clear}"
puts "#{Array1.clear}"

输出:

[]
[]
[]

参考: https://devdocs.io/ruby~2.5/array#method-i-clear