Class: RandomNameGenerator

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

Overview

RandomNameGenerator:

Examples

rng = RandomNameGenerator.new(RandomNameGenerator::GOBLIN)
puts rng.compose(3)

By default RandomNameGenerator uses the Fantasy syllable file and creates a name with between 2 and 5 syllables.

rng = RandomNameGenerator.new
puts rng.compose

Constant Summary collapse

ELVEN =
File.new("#{dirname}/languages/elven.txt")
FANTASY =
File.new("#{dirname}/languages/fantasy.txt")
GOBLIN =
File.new("#{dirname}/languages/goblin.txt")
ROMAN =
File.new("#{dirname}/languages/roman.txt")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = RandomNameGenerator::FANTASY, random: Random.new) ⇒ RandomNameGenerator


25
26
27
28
29
30
31
32
33
# File 'lib/random_name_generator/random_name_generator.rb', line 25

def initialize(filename = RandomNameGenerator::FANTASY, random: Random.new)
  @file = File.new(filename)
  @rnd = random
  @pre_syllables = []
  @sur_syllables = []
  @mid_syllables = []

  refresh
end

Instance Attribute Details

#mid_syllablesObject (readonly)

Returns the value of attribute mid_syllables


23
24
25
# File 'lib/random_name_generator/random_name_generator.rb', line 23

def mid_syllables
  @mid_syllables
end

#preObject (readonly)

Returns the value of attribute pre


23
24
25
# File 'lib/random_name_generator/random_name_generator.rb', line 23

def pre
  @pre
end

#pre_syllablesObject (readonly)

Returns the value of attribute pre_syllables


23
24
25
# File 'lib/random_name_generator/random_name_generator.rb', line 23

def pre_syllables
  @pre_syllables
end

#sur_syllablesObject (readonly)

Returns the value of attribute sur_syllables


23
24
25
# File 'lib/random_name_generator/random_name_generator.rb', line 23

def sur_syllables
  @sur_syllables
end

Class Method Details

.flip_modeObject

Public: Static factory method that instantiates a RandomNameGenerator in a random language.


36
37
38
39
40
41
42
# File 'lib/random_name_generator/random_name_generator.rb', line 36

def self.flip_mode
  langs = [RandomNameGenerator::FANTASY,
           RandomNameGenerator::ELVEN,
           RandomNameGenerator::GOBLIN,
           RandomNameGenerator::ROMAN]
  new(langs.sample)
end

.pick_number_of_syllablesObject


53
54
55
# File 'lib/random_name_generator/random_name_generator.rb', line 53

def self.pick_number_of_syllables
  [2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5].sample
end

Instance Method Details

#compose(count = RandomNameGenerator.pick_number_of_syllables) ⇒ Object


44
45
46
47
48
49
50
51
# File 'lib/random_name_generator/random_name_generator.rb', line 44

def compose(count = RandomNameGenerator.pick_number_of_syllables)
  @pre = pre_syllables.sample
  return @pre.to_s.capitalize if count < 2

  name = determine_middle_syllables(count - 2, pre)
  name << determine_last_syllable(name.last)
  name.map(&:to_s).join.capitalize
end

#to_sObject


57
58
59
# File 'lib/random_name_generator/random_name_generator.rb', line 57

def to_s
  "NameGenerator (#{@file.path})"
end