Class: Tooled::Stack
Instance Method Summary collapse
- #<<(value) ⇒ Object (also: #push)
- #[](index) ⇒ Object
- #count ⇒ Object (also: #length)
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Stack
constructor
A new instance of Stack.
- #peek ⇒ Object
- #pop ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ Stack
Returns a new instance of Stack.
8 9 10 |
# File 'lib/tooled/stack.rb', line 8 def initialize @stack_values = [] end |
Instance Method Details
#<<(value) ⇒ Object Also known as: push
17 18 19 |
# File 'lib/tooled/stack.rb', line 17 def <<(value) @stack_values << value end |
#[](index) ⇒ Object
30 31 32 |
# File 'lib/tooled/stack.rb', line 30 def [](index) @stack_values[@stack_values.count - (index.to_i + 1)] end |
#count ⇒ Object Also known as: length
12 13 14 |
# File 'lib/tooled/stack.rb', line 12 def count @stack_values.count end |
#each(&block) ⇒ Object
42 43 44 |
# File 'lib/tooled/stack.rb', line 42 def each(&block) yield block.call(pop) until empty? end |
#empty? ⇒ Boolean
38 39 40 |
# File 'lib/tooled/stack.rb', line 38 def empty? @stack_values.empty? end |
#peek ⇒ Object
22 23 24 |
# File 'lib/tooled/stack.rb', line 22 def peek @stack_values.last end |
#pop ⇒ Object
26 27 28 |
# File 'lib/tooled/stack.rb', line 26 def pop @stack_values.delete_at(@stack_values.count - 1) end |
#to_a ⇒ Object
34 35 36 |
# File 'lib/tooled/stack.rb', line 34 def to_a @stack_values.dup end |