Class: NameGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/name-generator.rb

Constant Summary collapse

VERSION =
'0.0.1'
DEFAULTS =
{
  :female_names => File.join(File.dirname(__FILE__),'name-generator','female-names'),
  :male_names   => File.join(File.dirname(__FILE__),'name-generator','male-names'),
  :surnames     => File.join(File.dirname(__FILE__),'name-generator','surnames')
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NameGenerator

Returns a new instance of NameGenerator.



14
15
16
17
18
19
# File 'lib/name-generator.rb', line 14

def initialize(options={})
  options = DEFAULTS.merge(options)
  @female_names = options[:female_names]
  @male_names   = options[:male_names]
  @surnames     = options[:surnames]
end

Instance Attribute Details

#female_namesObject

Returns the value of attribute female_names.



4
5
6
# File 'lib/name-generator.rb', line 4

def female_names
  @female_names
end

#male_namesObject

Returns the value of attribute male_names.



4
5
6
# File 'lib/name-generator.rb', line 4

def male_names
  @male_names
end

#surnamesObject

Returns the value of attribute surnames.



4
5
6
# File 'lib/name-generator.rb', line 4

def surnames
  @surnames
end

Instance Method Details

#female_nameObject



21
22
23
24
25
26
# File 'lib/name-generator.rb', line 21

def female_name
  open(@female_names) do |f|
    l = f.readlines
    l[rand(l.size)].chomp.match(/^\w+/).to_s
  end
end

#first_nameObject Also known as: middle_name



35
36
37
# File 'lib/name-generator.rb', line 35

def first_name
  rand(2) == 1 ? male_name : female_name
end

#full_nameObject



46
47
48
# File 'lib/name-generator.rb', line 46

def full_name
  "#{first_name} #{first_name} #{surname}"
end

#male_nameObject



28
29
30
31
32
33
# File 'lib/name-generator.rb', line 28

def male_name
  open(@male_names) do |f|
    l = f.readlines
    l[rand(l.size)].chomp.match(/^\w+/).to_s
  end
end

#surnameObject Also known as: last_name



39
40
41
42
43
44
# File 'lib/name-generator.rb', line 39

def surname
  open(@surnames) do |f|
    l = f.readlines
    l[rand(l.size)].chomp.match(/^\w+/).to_s
  end
end