delete?()是Ruby中的內置方法,該方法從集合中刪除給定對象並返回self對象。如果對象不存在,則返回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
相關用法
- Ruby Set delete()用法及代碼示例
- Ruby Hash delete()用法及代碼示例
- Ruby String delete!用法及代碼示例
- Ruby String delete()用法及代碼示例
- Ruby Array delete()用法及代碼示例
- Ruby Set add?用法及代碼示例
- Ruby SizedQueue deq()用法及代碼示例
- Ruby SizedQueue enq()用法及代碼示例
- Ruby Math cos()用法及代碼示例
- Ruby Set member?()用法及代碼示例
- Ruby SizedQueue max()用法及代碼示例
- Scala Date -用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Set delete? function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。