Ruby 集合 delete?函数
delete?() 是Ruby中的一个内置方法,它从集合中删除给定的对象并返回自己的对象。如果该对象不存在,它将返回 nil。
语法 : s1.name.delete(object)
参数 :该函数需要一个强制性的参数,即要删除的对象。
返回值 : 从集合中删除对象后,它将返回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