Class: Whitespace::Stack
- Inherits:
-
Object
- Object
- Whitespace::Stack
- Defined in:
- lib/whitespace/data_structures/stack.rb
Instance Method Summary collapse
-
#initialize ⇒ Stack
constructor
A new instance of Stack.
- #pop ⇒ Object
- #push(x) ⇒ Object
- #size ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize ⇒ Stack
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
#pop ⇒ Object
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 |
#size ⇒ Object
21 22 23 |
# File 'lib/whitespace/data_structures/stack.rb', line 21 def size @elements.size end |
#top ⇒ Object
16 17 18 19 |
# File 'lib/whitespace/data_structures/stack.rb', line 16 def top return @elements.last unless @elements.empty? raise EmptyError end |