Class: NameGenerator
- Inherits:
-
Object
- Object
- NameGenerator
- Defined in:
- lib/name_generator.rb
Instance Method Summary collapse
- #generate_name(word) ⇒ Object
- #generate_names(start_pairs, count = 10) ⇒ Object
-
#initialize(follower_letters, min_length = 3, max_length = 9) ⇒ NameGenerator
constructor
A new instance of NameGenerator.
Constructor Details
#initialize(follower_letters, min_length = 3, max_length = 9) ⇒ NameGenerator
Returns a new instance of NameGenerator.
2 3 4 5 6 |
# File 'lib/name_generator.rb', line 2 def initialize(follower_letters, min_length = 3, max_length = 9) @min_word_length = min_length @max_word_length = max_length @follower_letters = follower_letters end |
Instance Method Details
#generate_name(word) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/name_generator.rb', line 8 def generate_name(word) last_pair = word[-2, 2] letter = @follower_letters[last_pair].slice(rand(@follower_letters[last_pair].length), 1) if word =~ /\s$/ return word[0, @max_word_length] unless word.length <= @min_word_length return generate_name(word[-1, 1]+letter) else word = word.gsub(/^\s/, '') return generate_name(word+letter) end end |
#generate_names(start_pairs, count = 10) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/name_generator.rb', line 20 def generate_names(start_pairs, count = 10) names = [] count.times do |i| names.push(generate_name(start_pairs[rand start_pairs.length]).capitalize) end return names end |