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, #update_sum_count, #weight

Constructor Details

This class inherits a constructor from Teebo::Base

Instance Method Details

#given_name(sex) ⇒ Object

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



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/teebo/name.rb', line 21

def given_name sex
  find_range_query = <<-SQL
    select * from given_names where sex = ?
      and (count_to - ?) >= 0
      order by id limit 1
  SQL

  count = sum_count sex
  rand = rand(count)
  return @@database.execute(find_range_query, sex, rand)[0]['name']
end

#sum_count(sex) ⇒ Object

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



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

def sum_count sex
  find_count = <<-SQL
    select sum(count) from 'given_names' where sex = ?
  SQL
  return @@database.execute(find_count, sex)[0][0]
end