Class: Stack

Inherits:
List show all
Defined in:
lib/stack.rb

Overview

Stack class implementing a typical stack data structure

Instance Method Summary collapse

Methods inherited from List

#push, #to_s

Constructor Details

#initialize(size = nil) ⇒ Stack

Returns a new instance of Stack.



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

def initialize(size = nil)
  super(size)
end

Instance Method Details

#popObject

This method implements pop functionality of a stack (LIFO)

Example:

>> s = Stack.new
>> s.push("else")
>> s.push("something")
>> s.pop()

> “something”



20
21
22
23
24
25
26
# File 'lib/stack.rb', line 20

def pop()
  if(@list.length == 0)
    raise CollectionErrors::StackUnderflowError.new
  else
    @list.pop
  end
end