class RandomShakespeare::Lines

Public Class Methods

new(parser = nil) click to toggle source
# File lib/random_shakespeare/lines.rb, line 5
def initialize(parser = nil)
  @parser = parser || Parser.new(Downloader::FILENAME)
  load_lines!
end

Public Instance Methods

next() click to toggle source
# File lib/random_shakespeare/lines.rb, line 10
def next
  load_lines! if lines_empty?
  random_line
end

Private Instance Methods

clean_up(speaker, speaker_lines) click to toggle source
# File lib/random_shakespeare/lines.rb, line 25
def clean_up(speaker, speaker_lines)
  if speaker_lines.count == 0
    @lines.delete(speaker)
  else
    @lines[speaker] = speaker_lines
  end
end
lines_empty?() click to toggle source
# File lib/random_shakespeare/lines.rb, line 33
def lines_empty?
  @lines == {}
end
load_lines!() click to toggle source
# File lib/random_shakespeare/lines.rb, line 37
def load_lines!
  @lines = @parser.all_lines
end
random_line() click to toggle source
# File lib/random_shakespeare/lines.rb, line 17
def random_line
  rand_speaker = @lines.keys[rand(@lines.keys.count)]
  speaker_lines = @lines[rand_speaker]
  line = speaker_lines.delete_at(rand(speaker_lines.count))
  clean_up(rand_speaker, speaker_lines)
  line
end