Class: NameGenerator
- Inherits:
-
Object
- Object
- NameGenerator
- Defined in:
- lib/randomizer/name_generator.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ NameGenerator
constructor
A new instance of NameGenerator.
- #parse_name_file(filename) ⇒ Object
- #random(options = {}) ⇒ Object
- #random_first_name(options = {}) ⇒ Object
- #random_last_name(options = {}) ⇒ Object
Constructor Details
#initialize ⇒ NameGenerator
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
.instance ⇒ Object
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( = {}) [random_first_name(), random_last_name()] 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( = {}) case [: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( = {}) @last_names.random end |