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

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

红宝石 |设置 delete()函数

delete()是 Ruby 中的一个内置方法,它从集合中删除给定对象并返回 self 对象。如果对象不存在,则仅返回 self。

示例 1

#Ruby program to illustrate the
#delete method
  
#requires the set
require "set"
  
    s1
    = Set[1, 2, 3]
  
#deletes 2 and prints self
      puts s1.delete(2)
  
#deletes 1 and prints self
          puts s1.delete(1)
  
#deletes 4 and prints self
              puts s1.delete(4)

输出

Set: {1, 3}
Set: {3}
Set: {3}

示例 2

#Ruby program to illustrate the
#delete method
  
#requires the set
require "set"
  
    s1
    = Set[11, 12, 33, "a"]
  
#deletes 'a' and prints self
      puts s1.delete("a")
  
#deletes 11 and prints self
          puts s1.delete(11)
  
#deletes 33 and prints self
              puts s1.delete(33)

输出

Set: {11, 12, 33}
Set: {12, 33}
Set: {12}

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