Sha256: 615794e54fe5dc8ad7aceb7cc67673632c4cc5d65dd95740d23dda2f8791c23d

Contents?: true

Size: 699 Bytes

Versions: 3

Compression:

Stored size: 699 Bytes

Contents

module Basic101

  class ForStack

    def initialize
      @stack = []
    end

    def empty?
      @stack.empty?
    end

    def push(statement)
      if has_reference?(statement.reference)
        raise DuplicateReferenceInForStack
      end
      @stack.push statement
    end

    def pop
      @stack.pop
    end

    def top
      @stack.last
    end

    def delete_reference(reference)
      @stack.reject! do |statement|
        statement.reference == reference
      end
    end

    def [](reference)
      @stack.find do |statement|
        statement.reference == reference
      end
    end

    private

    def has_reference?(reference)
      !!self[reference]
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
basic101-0.4.0 lib/basic101/for_stack.rb
basic101-0.2.0 lib/basic101/for_stack.rb
basic101-0.1.0 lib/basic101/for_stack.rb