Class: Grizzled::Stack

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

Overview

A simple stack wrapper on top of a Ruby array, providing a little more protection that using an array directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pop_empty_nil = true) ⇒ Stack

Initialize a new stack.

Parameters:

pop_empty_nil

true if popping an empty stack should just return nil, false if it should thrown an exception.



58
59
60
61
# File 'lib/grizzled/stack.rb', line 58

def initialize(pop_empty_nil=true)
  @the_stack = []
  @pop_empty_nil = pop_empty_nil
end

Instance Attribute Details

#pop_empty_nilObject (readonly)

Returns the value of attribute pop_empty_nil.



50
51
52
# File 'lib/grizzled/stack.rb', line 50

def pop_empty_nil
  @pop_empty_nil
end

Instance Method Details

#<=>(other) ⇒ Object

Compare this stack to another element.



149
150
151
152
153
154
155
# File 'lib/grizzled/stack.rb', line 149

def <=>(other)
  if other.class == Stack
    other.to_a <=> to_a
  else
    other.to_s <=> this.to_s
  end
end

#clearObject

Clear the stack. Returns the stack, for chaining.



123
124
125
126
# File 'lib/grizzled/stack.rb', line 123

def clear
  @the_stack.clear
  self
end

#eachObject

Yield each element of the stack, in turn. Unaffected by a change in the stack.



116
117
118
119
120
# File 'lib/grizzled/stack.rb', line 116

def each
  self.to_a.each do |element|
    yield element
  end
end

#eql?(other) ⇒ Boolean

Determine if this hash is equal to another one.

Returns:

  • (Boolean)


144
145
146
# File 'lib/grizzled/stack.rb', line 144

def eql?(other)
  (other.class == Stack) and (other.to_a == to_a)
end

#hashObject

Return the stack’s hash.



139
140
141
# File 'lib/grizzled/stack.rb', line 139

def hash
  @the_stack.hash
end

#inspectObject

Printable version.



129
130
131
# File 'lib/grizzled/stack.rb', line 129

def inspect
  @the_stack.inspect
end

#is_empty?Boolean

Convenience method for length == 0.

Returns:

  • (Boolean)


82
83
84
# File 'lib/grizzled/stack.rb', line 82

def is_empty?
  length == 0
end

#lengthObject

Returns the size of the stack.



110
111
112
# File 'lib/grizzled/stack.rb', line 110

def length
  @the_stack.length
end

#popObject

Pop the top element from the stack. If the stack is empty, this method throws a StackUnderflowException, if pop_empty_nil is false; or returns nil, if pop_empty_nil is true.



66
67
68
69
70
71
# File 'lib/grizzled/stack.rb', line 66

def pop
  if (@the_stack.length == 0) && (not @pop_empty_nil)
    raise StackUnderflowException.new
  end
  @the_stack.pop
end

#pop_allObject

Pop every element of the stack, returning the results as an array and clearing the stack.



75
76
77
78
79
# File 'lib/grizzled/stack.rb', line 75

def pop_all
  result = @the_stack.reverse
  @the_stack.clear
  result
end

#push(element) ⇒ Object

Push an element or an array of elements onto the stack. Returns the stack itself, to allow chaining. Note: If you push an array of elements, the elements end up being reversed on the stack. That is, this:

stack = Stack.new.push([1, 2, 3]) # yields Stack[3, 2, 1]

is equivalent to

stack = Stack.new
[1, 2, 3].each {|i| stack.push i}

Parameters:

element

The object to push onto the stack.



100
101
102
103
104
105
106
107
# File 'lib/grizzled/stack.rb', line 100

def push(element)
  if element.class == Array
    element.each {|e| @the_stack.push e}
  else
    @the_stack.push element
  end
  self
end

#to_aObject

Return the stack as an array.



134
135
136
# File 'lib/grizzled/stack.rb', line 134

def to_a
  @the_stack.reverse
end