📜  红宝石 |设置删除?函数

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

红宝石 |设置删除?函数

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

示例 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}

示例 2

#Ruby program to illustrate the
#delete ? method
  
#requires the set
require "set"
  
    s1
    = Set[4, 7, 13, "q"]
  
#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
    ? (13)

输出

Set: {4, 7, "q"}

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