Sha256: 0f007f7e6b06e0e757200a0fcbb9ff340b957bf1a899db81e351526ff810bba5

Contents?: true

Size: 584 Bytes

Versions: 1

Compression:

Stored size: 584 Bytes

Contents

require 'thread'

class PirateGame::LogBook

  include Enumerable

  def initialize size = 10
    @mutex = Mutex.new
    @log_book = []
    @size = size
  end

  def add entry, author = 'unknown'
    @mutex.synchronize do
      @log_book << [entry, author]

      @log_book.shift if @log_book.size > @size
    end
  end

  def each
    return enum_for __method__ unless block_given?

    @log_book.each do |(entry, author)|
      yield [entry, author]
    end
  end

  def empty?
    @log_book.empty?
  end

  def length
    @log_book.length
  end
  alias_method :size, :length

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pirate_game-0.0.1 lib/pirate_game/log_book.rb