Class: Redd::Clients::Base::Stream::PRAWBoundedQueueSet
- Inherits:
-
Set
- Object
- Set
- Redd::Clients::Base::Stream::PRAWBoundedQueueSet
- Defined in:
- lib/redd/clients/base/stream.rb
Overview
A class similar to PRAW’s implementation of a BoundedSet.
Instance Method Summary collapse
-
#dequeue ⇒ Object
(also: #deq)
Remove the last element of the queue.
-
#enqueue(element) ⇒ PRAWBoundedQueueSet
(also: #enq)
Add an element to the front if it isn’t already in the queue.
-
#enqueue?(element) ⇒ Boolean
(also: #enq?)
Add an element to the front if it isn’t already in the queue.
-
#initialize(max, *args, &block) ⇒ PRAWBoundedQueueSet
constructor
A new instance of PRAWBoundedQueueSet.
Constructor Details
#initialize(max, *args, &block) ⇒ PRAWBoundedQueueSet
Returns a new instance of PRAWBoundedQueueSet.
10 11 12 13 14 |
# File 'lib/redd/clients/base/stream.rb', line 10 def initialize(max, *args, &block) @max = max @queue = [] super(*args, &block) end |
Instance Method Details
#dequeue ⇒ Object Also known as: deq
Remove the last element of the queue.
41 42 43 44 45 |
# File 'lib/redd/clients/base/stream.rb', line 41 def dequeue element = @queue.shift delete(element) element end |
#enqueue(element) ⇒ PRAWBoundedQueueSet Also known as: enq
Add an element to the front if it isn’t already in the queue.
19 20 21 22 23 |
# File 'lib/redd/clients/base/stream.rb', line 19 def enqueue(element) @queue.push(element) if add?(element) dequeue! if size > @max self end |
#enqueue?(element) ⇒ Boolean Also known as: enq?
Add an element to the front if it isn’t already in the queue.
29 30 31 32 33 34 35 36 |
# File 'lib/redd/clients/base/stream.rb', line 29 def enqueue?(element) added = add?(element) if added @queue.push(element) dequeue if size > @max end added end |