JSQueue 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/js_queue.rb +28 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a77f351086b55cbfdf216ab768fa0ca077d99a6
|
4
|
+
data.tar.gz: fa14fd8c07d7df933da37ba2f9ba106b8d5c8a27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 461c4b61f32090cfc175004809b8093390efbfefe7e1b26d4350f4b901f38a6eb949bef3b27ef4bf715824d8fa24d031b6be044f0f3655c352b2afdca3396a30
|
7
|
+
data.tar.gz: b931f3ff762c8c8b682573ae2eb5d0f847d049a3c889f9e4a79e94e90208f1bc3fe239bc157c935ffa6a7d5e3aadb6d8e771ba6b467b2349e8ae436c65b1a839
|
data/lib/js_queue.rb
CHANGED
@@ -3,11 +3,22 @@ require_relative 'queue_node'
|
|
3
3
|
class JSQueue
|
4
4
|
attr_accessor :first, :last
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
##
|
7
|
+
# Creates a new queue with an optional first item +data+.
|
8
|
+
#
|
9
|
+
# If an initial value is provided, it will be considered both the +first+ and
|
10
|
+
# +last+ item in the queue.
|
11
|
+
def initialize(data=nil)
|
12
|
+
@first = QueueNode.new data unless data.nil?
|
13
|
+
@first ||= nil
|
8
14
|
@last = @first
|
9
15
|
end
|
10
16
|
|
17
|
+
##
|
18
|
+
# Adds the provided value +data+ to the back of the queue.
|
19
|
+
#
|
20
|
+
# If it is the only item in the queue, it will be considered both the +first+
|
21
|
+
# and the +last+.
|
11
22
|
def enqueue(data)
|
12
23
|
node = QueueNode.new data
|
13
24
|
unless last.nil?
|
@@ -17,6 +28,11 @@ class JSQueue
|
|
17
28
|
@first = node if @first.nil?
|
18
29
|
end
|
19
30
|
|
31
|
+
##
|
32
|
+
# Removes and returns the data value of the +first+ item in the queue.
|
33
|
+
#
|
34
|
+
# == Example
|
35
|
+
# queue.dequeue # => 'I was first in line'
|
20
36
|
def dequeue
|
21
37
|
return if @first.nil?
|
22
38
|
data = @first.data
|
@@ -25,10 +41,20 @@ class JSQueue
|
|
25
41
|
data
|
26
42
|
end
|
27
43
|
|
44
|
+
##
|
45
|
+
# Returns the +first+ enqueued item's value, without removing it.
|
46
|
+
#
|
47
|
+
# == Example
|
48
|
+
# queue.peek # => 'I am first in line'
|
28
49
|
def peek
|
29
50
|
first.nil? ? nil : first.data
|
30
51
|
end
|
31
52
|
|
53
|
+
##
|
54
|
+
# Returns a Boolean indicating whether the queue has any items.
|
55
|
+
#
|
56
|
+
# == Example
|
57
|
+
# queue.empty? # => false
|
32
58
|
def empty?
|
33
59
|
first.nil?
|
34
60
|
end
|