Class: PirateGame::LogBook

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pirate_game/log_book.rb

Instance Method Summary collapse

Constructor Details

#initialize(size = 10) ⇒ LogBook

Returns a new instance of LogBook.



7
8
9
10
11
# File 'lib/pirate_game/log_book.rb', line 7

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

Instance Method Details

#add(entry, author = 'unknown') ⇒ Object



13
14
15
16
17
18
19
# File 'lib/pirate_game/log_book.rb', line 13

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

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

#eachObject



21
22
23
24
25
26
27
# File 'lib/pirate_game/log_book.rb', line 21

def each
  return enum_for __method__ unless block_given?

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

#empty?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/pirate_game/log_book.rb', line 29

def empty?
  @log_book.empty?
end

#lengthObject Also known as: size



33
34
35
# File 'lib/pirate_game/log_book.rb', line 33

def length
  @log_book.length
end