require 'stack_contract' class Stack contract StackContract class NoPopForEmptyStack < RuntimeError end def initialize @objects = [] end def initialize_copy(orig) @objects = orig.objects end def objects @objects.dup end def size @objects.size end # doctest: A new Stack should be empty. # >> stack = Stack.new # >> stack.empty? # => true def empty? size == 0 end # doctest: A new Stack should have no object on top. # >> stack = Stack.new # >> begin # ?> stack.top # ?> rescue Test::Unit::AssertionFailedError # ?> true # ?> else # ?> false # ?> end # => true def top @objects.last end # doctest: From a new Stack no object can be popped. # >> stack = Stack.new # >> begin # ?> stack.pop # ?> rescue Test::Unit::AssertionFailedError # ?> true # ?> else # ?> false # ?> end # => true def pop @objects.pop end # doctest: A pushed object should be on the top. # >> stack = Stack.new # >> object = Object.new # >> stack.push(object) # >> stack.empty? # => false # >> stack.top # => object # >> stack.pop # => object # >> stack.empty? # => true def push(object) @objects.push(object) nil end end