Class: MaxStack
- Inherits:
-
Object
- Object
- MaxStack
- Defined in:
- lib/max_stack.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
-
#initialize ⇒ MaxStack
constructor
A new instance of MaxStack.
- #max ⇒ Object
- #pop ⇒ Object
- #push(val) ⇒ Object
Constructor Details
#initialize ⇒ MaxStack
Returns a new instance of MaxStack.
4 5 6 |
# File 'lib/max_stack.rb', line 4 def initialize @store = [] end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
2 3 4 |
# File 'lib/max_stack.rb', line 2 def store @store end |
Instance Method Details
#max ⇒ Object
21 22 23 |
# File 'lib/max_stack.rb', line 21 def max @store[-1][1] end |
#pop ⇒ Object
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 |