Class: Teebo::Name

Inherits:
Base
  • Object
show all
Defined in:
lib/teebo/name.rb

Overview

Generates names in accordance with their frequency in the United States population.

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Teebo::Base

Instance Method Details

#full_name(sex = nil) ⇒ Object

Generates a normal full name, including a middle name.

For now, this is fairly sloppy, as the probability that a middle name will simply be another given name of the same gender is almost certainly less than 100%.

TODO: Make this take into account different probablities of different

types of middle names.


29
30
31
32
33
34
# File 'lib/teebo/name.rb', line 29

def full_name sex=nil
  if sex.nil?
    sex = ['M', 'F'].sample
  end
  given_name(sex) + " " + given_name(sex) + " " + surname
end

#given_name(sex) ⇒ Object

Selects a random (weighted) given name from the database.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/teebo/name.rb', line 49

def given_name sex
  find_range_query = "    select * from given_names where sex = ?\n      and (count_to - ?) >= 0\n      order by id limit 1\n  SQL\n\n  count = sum_count sex\n  selection = rand(count)\n  @database.execute(find_range_query, sex, selection)[0]['name']\nend\n"

#name(sex = nil) ⇒ Object

Picks a random first & last name, selecting a random gender if it’s not specified.



12
13
14
15
16
17
# File 'lib/teebo/name.rb', line 12

def name sex=nil
  if sex.nil?
    sex = ['M', 'F'].sample
  end
  given_name(sex) + " " + surname
end

#sum_count(sex) ⇒ Object

Finds the total count for the number of names in the database.



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

def sum_count sex
  find_count = "    select sum(count) from 'given_names' where sex = ?\n  SQL\n  @database.execute(find_count, sex)[0][0]\nend\n"

#surnameObject

Selects a random (weighted) surname from the database.



64
65
66
67
68
69
70
71
72
# File 'lib/teebo/name.rb', line 64

def surname
  find_range_query = "    select * from surnames where (count_to - ?) >= 0 order by id limit 1\n  SQL\n\n  count = @database.execute('select sum(count) from surnames')[0][0]\n  selection = rand(count)\n  @database.execute(find_range_query, selection)[0]['name']\nend\n"