Module: GuessWhoNoFuzzy::NamesLoader

Defined in:
lib/guess_who_no_fuzzy/names_loader.rb

Class Method Summary collapse

Class Method Details

.load!Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/guess_who_no_fuzzy/names_loader.rb', line 3

def self.load!
  names_by_first_letter = {}

  filenames = [
    "files/initials.txt",
    "files/females.txt",
    "files/males.txt",
    "files/surnames.txt"
  ]

  filenames.each do |filename|
    path = File.join(File.dirname(__FILE__), '../../', filename)
    f = File.open(path, "r")
    f.each_line do |l|
      name = l.split(" ")[0]
      first_char = name[0]
      names_by_first_letter[first_char] ||= []
      names_by_first_letter[first_char] << name
    end
    f.close
  end

  @@names_by_first_letter = names_by_first_letter
end

.names_by_first_letter(letter) ⇒ Object



28
# File 'lib/guess_who_no_fuzzy/names_loader.rb', line 28

def self.names_by_first_letter(letter); @@names_by_first_letter[letter] || []; end