📜  红宝石 |设置 clear()函数

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

红宝石 |设置 clear()函数

clear()是 Ruby 中的一个内置方法,用于清除集合中的所有元素。

示例 1

#Ruby program to illustrate the clear method
  
#requires the set
require "set"
  
    s1
    = Set[2, 1]
  
#Prints s1
      puts s1
  
#Clears the set and returns self
#which is printed
          puts s1.clear

输出

Set: {2, 1}
Set: {}

示例 2

#Ruby program to illustrate the clear method
  
#requires the set
require "set"
  
    s1
    = Set[]
  
#Prints s1
      puts s1
  
#Add 10 to it
          s1
      << 10
  
         puts s1
  
#Clears the set and returns self
#which is printed
             puts s1.clear

输出

Set: {}
Set: {10}
Set: {}

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