-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetName.rb
38 lines (35 loc) · 1.06 KB
/
getName.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Provides his and her first and last names.
class Name
def first_name(gender)
genders = ['male', 'female']
unless genders.include?(gender)
return "Unknown"
end
begin
require "sqlite3"
db = SQLite3::Database.open "names.db"
first_name_query = db.prepare "SELECT * from humaniti_#{gender}_first ORDER BY RANDOM() LIMIT 1"
first_name_result = first_name_query.execute
first_name = first_name_result.first
ensure
first_name_query.close if first_name_query
db.close if db
end
return first_name[0].to_s
end
def last_name()
begin
require "sqlite3"
db = SQLite3::Database.open "names.db"
last_name_query = db.prepare "SELECT * from humaniti_last ORDER BY RANDOM() LIMIT 1"
last_name_result = last_name_query.execute
last_name = last_name_result.first
ensure
last_name_query.close if last_name_query
db.close if db
end
return last_name[0].to_s
end
end
namer = Name.new()
puts "#{namer.first_name('male')} and #{namer.first_name('female')} #{namer.last_name}"