Class: Containers::Stack
- Inherits:
-
Object
- Object
- Containers::Stack
- Includes:
- Enumerable
- Defined in:
- lib/containers/stack.rb
Overview
SOFTWARE.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterate over the Stack in LIFO order.
-
#empty? ⇒ Boolean
Returns true if the stack is empty, false otherwise.
-
#initialize(ary = []) ⇒ Stack
constructor
Create a new stack.
-
#next ⇒ Object
Returns the next item from the stack but does not remove it.
-
#pop ⇒ Object
Removes the next item from the stack and returns it.
-
#push(obj) ⇒ Object
(also: #<<)
Adds an item to the stack.
-
#size ⇒ Object
Return the number of items in the stack.
Constructor Details
#initialize(ary = []) ⇒ Stack
Create a new stack. Takes an optional array argument to initialize the stack.
s = Containers::Stack.new([1, 2, 3])
s.pop #=> 3
s.pop #=> 2
37 38 39 |
# File 'lib/containers/stack.rb', line 37 def initialize(ary=[]) @container = Containers::Deque.new(ary) end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over the Stack in LIFO order.
84 85 86 |
# File 'lib/containers/stack.rb', line 84 def each(&block) @container.each_backward(&block) end |
#empty? ⇒ Boolean
Returns true if the stack is empty, false otherwise.
79 80 81 |
# File 'lib/containers/stack.rb', line 79 def empty? @container.empty? end |
#next ⇒ Object
Returns the next item from the stack but does not remove it.
s = Containers::Stack.new([1, 2, 3])
s.next #=> 3
s.size #=> 3
46 47 48 |
# File 'lib/containers/stack.rb', line 46 def next @container.back end |
#pop ⇒ Object
Removes the next item from the stack and returns it.
s = Containers::Stack.new([1, 2, 3])
s.pop #=> 3
s.size #=> 2
66 67 68 |
# File 'lib/containers/stack.rb', line 66 def pop @container.pop_back end |
#push(obj) ⇒ Object Also known as: <<
Adds an item to the stack.
s = Containers::Stack.new([1])
s.push(2)
s.pop #=> 2
s.pop #=> 1
56 57 58 |
# File 'lib/containers/stack.rb', line 56 def push(obj) @container.push_back(obj) end |
#size ⇒ Object
Return the number of items in the stack.
s = Containers::Stack.new([1, 2, 3])
s.size #=> 3
74 75 76 |
# File 'lib/containers/stack.rb', line 74 def size @container.size end |