Sha256: dc3b48cb56be5adf2bf71bd1e4593fc56f61e4897712b743c79d5b58ece29aee

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
armin-joellenbeck-rdbc-0.0.6 examples/stack.rb