Class: Whitespace::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/whitespace/data_structures/stack.rb

Instance Method Summary collapse

Constructor Details

#initializeStack

Returns a new instance of Stack.



3
4
5
# File 'lib/whitespace/data_structures/stack.rb', line 3

def initialize
  @elements = []
end

Instance Method Details

#popObject

Raises:



11
12
13
14
# File 'lib/whitespace/data_structures/stack.rb', line 11

def pop
  return @elements.pop unless @elements.empty?
  raise EmptyError
end

#push(x) ⇒ Object



7
8
9
# File 'lib/whitespace/data_structures/stack.rb', line 7

def push(x)
  @elements.push x
end

#sizeObject



21
22
23
# File 'lib/whitespace/data_structures/stack.rb', line 21

def size
  @elements.size
end

#topObject

Raises:



16
17
18
19
# File 'lib/whitespace/data_structures/stack.rb', line 16

def top
  return @elements.last unless @elements.empty?
  raise EmptyError
end