Class: NameGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/randomizer/name_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNameGenerator

Returns a new instance of NameGenerator.



7
8
9
10
11
12
# File 'lib/randomizer/name_generator.rb', line 7

def initialize
  @female_first_names = parse_name_file('female_first_names.txt') # Top 500 female names.
  @male_first_names = parse_name_file('male_first_names.txt') # Top 500 male names.
  @last_names = parse_name_file('last_names.txt') # Top 3000 last names.
  @first_names = @female_first_names + @male_first_names
end

Class Method Details

.instanceObject



3
4
5
# File 'lib/randomizer/name_generator.rb', line 3

def self.instance
  @@instance ||= NameGenerator.new
end

Instance Method Details

#parse_name_file(filename) ⇒ Object



14
15
16
# File 'lib/randomizer/name_generator.rb', line 14

def parse_name_file(filename)
  File.readlines(File.join(DB_DIR, filename)).collect {|line| line.strip}
end

#random(options = {}) ⇒ Object



18
19
20
# File 'lib/randomizer/name_generator.rb', line 18

def random(options = {})
  [random_first_name(options), random_last_name(options)]
end

#random_first_name(options = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/randomizer/name_generator.rb', line 22

def random_first_name(options = {})
  case options[:gender]
    when 'm', 'male' then @male_first_names.random
    when 'f', 'female' then @female_first_names.random
    else @first_names.random
  end
end

#random_last_name(options = {}) ⇒ Object



30
31
32
# File 'lib/randomizer/name_generator.rb', line 30

def random_last_name(options = {})
  @last_names.random
end