空嗎? Ruby中的內置函數是檢查隊列是否為空?它返回一個布爾值,如果Queue為空或返回false,則為true。
用法:q_name.empty?
參數:該函數不包含任何元素。
返回值:如果隊列為空,則返回true,否則返回false。
例子1:
#Ruby program for empty ? function in Queue
#Create a new QUEUE q1
q1 = Queue.new
#push 5
q1.push(5)
#push 6
q1.push(6)
#Checks if the queue is empty or not
puts q1.empty
?
#Clears the queue
q1.clear()
#Checks if the queue is empty or not
puts q1.empty
?
輸出:
false true
例子2:
#Ruby program for empty ? function in Queue
#Create a new QUEUE q1
q1 = Queue.new
#Checks if the queue is empty or not
puts q1.empty
?
輸出:
true
參考:https://devdocs.io/ruby~2.5/queue#method-i-clear
相關用法
- Ruby Set empty?()用法及代碼示例
- Ruby Hash empty?用法及代碼示例
- Ruby Matrix empty?()用法及代碼示例
- Ruby SizedQueue empty?用法及代碼示例
- Ruby StringScanner empty?用法及代碼示例
- Ruby Symbol empty?用法及代碼示例
- Ruby Queue new()用法及代碼示例
- Ruby Queue pop()用法及代碼示例
- Ruby Queue deq()用法及代碼示例
- Ruby Queue enq()用法及代碼示例
- Ruby Queue clear()用法及代碼示例
- Ruby Queue shift()用法及代碼示例
- Ruby Queue size()用法及代碼示例
- Ruby Queue length()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Queue empty? function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。