Class: Birthcert::Name

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

Instance Method Summary collapse

Constructor Details

#initialize(size = 500) ⇒ Name

note – we can guarantee the names will be in a certain range by simply reading a set number of lines from the name files into memory and using that as the data set



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/birthcert/name.rb', line 6

def initialize(size=500)
  @size = size
  @first_names = Array.new
  @last_names = Array.new

  # read data out of files that come with this gem
  fpat = File.expand_path('../data/first_names.txt', __FILE__)
  lpat = File.expand_path('../data/last_names.txt', __FILE__)

  File.open(fpat, 'r').each_line do |line|
    @first_names << line.chomp
  end
  File.open(lpat, 'r').each_line do |line|
    @last_names << line.chomp
  end
end

Instance Method Details

#first(amount = 1) ⇒ Object

get random first and last names from the data set



24
25
26
# File 'lib/birthcert/name.rb', line 24

def first(amount=1)
  return @first_names.sample(amount)
end

#full(sep = ' ') ⇒ Object

when getting a full name, we can specify if and when we don’t want to separate them with whitespace, ie. “first-last”



33
34
35
# File 'lib/birthcert/name.rb', line 33

def full(sep=' ')
  return "#{@first_names.sample(1).pop}#{sep}#{@last_names.sample(1).pop}"
end

#last(amount = 1) ⇒ Object



27
28
29
# File 'lib/birthcert/name.rb', line 27

def last(amount=1)
  return @last_names.sample(amount)
end