Class: Tooled::Stack

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

Instance Method Summary collapse

Constructor Details

#initializeStack

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

#countObject 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

Returns:

  • (Boolean)


38
39
40
# File 'lib/tooled/stack.rb', line 38

def empty?
  @stack_values.empty?
end

#peekObject



22
23
24
# File 'lib/tooled/stack.rb', line 22

def peek
  @stack_values.last
end

#popObject



26
27
28
# File 'lib/tooled/stack.rb', line 26

def pop
  @stack_values.delete_at(@stack_values.count - 1)
end

#to_aObject



34
35
36
# File 'lib/tooled/stack.rb', line 34

def to_a
  @stack_values.dup
end