Class: MaxStack

Inherits:
Object
  • Object
show all
Defined in:
lib/max_stack.rb

Direct Known Subclasses

DataStruct::MaxStack

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMaxStack

Returns a new instance of MaxStack.


4
5
6
# File 'lib/max_stack.rb', line 4

def initialize
  @store = []
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store


2
3
4
# File 'lib/max_stack.rb', line 2

def store
  @store
end

Instance Method Details

#maxObject


21
22
23
# File 'lib/max_stack.rb', line 21

def max
  @store[-1][1]
end

#popObject


17
18
19
# File 'lib/max_stack.rb', line 17

def pop
  @store.pop[0]
end

#push(val) ⇒ Object


8
9
10
11
12
13
14
15
# File 'lib/max_stack.rb', line 8

def push(val)
  if @store.empty?
    @store << [val, val]
  else
    max_val = [val, self.max].max
    @store << [val, max_val]
  end
end