Class: Containers::Stack

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/containers/stack.rb

Overview

SOFTWARE.

Instance Method Summary collapse

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.

Returns:

  • (Boolean)


79
80
81
# File 'lib/containers/stack.rb', line 79

def empty?
  @container.empty?
end

#nextObject

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

#popObject

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

#sizeObject

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