红宝石 |设置删除?函数
delete?()是 Ruby 中的一个内置方法,它从集合中删除给定对象并返回 self 对象。如果对象不存在,则返回nil 。
Syntax: s1.name.delete(object)
Parameters: The function takes a mandatory parameter object which is to be deleted.
Return Value: It returns self after deletion of the object from the set, and it returns nil if the object is not present.
示例 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