Class: MemorableStrings::Phoneme
- Inherits:
-
Object
- Object
- MemorableStrings::Phoneme
- Defined in:
- lib/memorable_strings/phoneme.rb
Overview
A unit of sound that can be used to build a larger string. Phonemes have no semantic meaning themselves, but have sound types and characteristics associated with them that are helpful in building a discernable word.
Class Attribute Summary collapse
-
.all ⇒ Object
readonly
The collection of phonemes.
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
The number of characters.
-
#value ⇒ Object
readonly
The character(s) representing this phoneme.
Class Method Summary collapse
-
.add(*values) ⇒ Object
Adds a new phoneme of the current class’s type.
-
.first(maxlength, &block) ⇒ Object
Generates a random phoneme of at most the given length.
-
.random(maxlength = nil, &block) ⇒ Object
Generates a random phoneme for the current class of at most the given length.
Instance Method Summary collapse
-
#first? ⇒ Boolean
Is this allowed to be the first in a sequence of phonemes?.
-
#initialize(value, options = {}) ⇒ Phoneme
constructor
Creates a new phoneme with the given value and configuration options.
-
#matches? ⇒ Boolean
Does this phoneme match the conditions specified by the block?.
-
#print_friendly?(context) ⇒ Boolean
Is this character unambiguous with other characters?.
Constructor Details
#initialize(value, options = {}) ⇒ Phoneme
Creates a new phoneme with the given value and configuration options.
Configuration options:
-
<tt>:first</strong> - Whether it can be used as the first value in a string. Default is true.
-
<tt>:print_friendly</strong> - Whether the characters are unambiguous when printed. This can be set to one of the following values:
-
true- Always print-friendly (default) -
:downcase- Only print-friendly when in lower case -
:upcase- Only print-friendly when in upper case -
false- Never print-friendly
-
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/memorable_strings/phoneme.rb', line 89 def initialize(value, = {}) invalid_keys = .keys - [:first, :print_friendly] raise ArgumentError, "Invalid key(s): #{invalid_keys.join(', ')}" unless invalid_keys.empty? = {:first => true, :print_friendly => true}.merge() @value = value.to_s @length = @value.length @first = [:first] @print_friendly = [:print_friendly] end |
Class Attribute Details
.all ⇒ Object (readonly)
The collection of phonemes
8 9 10 |
# File 'lib/memorable_strings/phoneme.rb', line 8 def all @all end |
Instance Attribute Details
#length ⇒ Object (readonly)
The number of characters
76 77 78 |
# File 'lib/memorable_strings/phoneme.rb', line 76 def length @length end |
#value ⇒ Object (readonly)
The character(s) representing this phoneme
73 74 75 |
# File 'lib/memorable_strings/phoneme.rb', line 73 def value @value end |
Class Method Details
.add(*values) ⇒ Object
Adds a new phoneme of the current class’s type.
See Phoneme#new for more information.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/memorable_strings/phoneme.rb', line 13 def add(*values) = values.last.is_a?(Hash) ? values.pop : {} values.flatten! values.map! do |value| (@all ||= []) << value = new(value, ) value end values.length == 1 ? values.first : values end |
.first(maxlength, &block) ⇒ Object
Generates a random phoneme of at most the given length. This will only randomly choose from the following sounds that are allowed to be the first character:
-
Vowel
-
Consonant
Examples
MemorableStrings::Phoneme.first(1)
#<MemorableStrings::Consonant:0xb7c38248 @first=true, @value="x", @length=1>
MemorableStrings::Phoneme.first(2)
#<MemorableStrings::Vowel:0xb7c3e2b0 @first=true, @value="ae", @length=2>
65 66 67 68 69 |
# File 'lib/memorable_strings/phoneme.rb', line 65 def first(maxlength, &block) (rand(2) == 1 ? Vowel : Consonant).random(maxlength) do |phoneme| phoneme.first? && phoneme.matches?(&block) end end |
.random(maxlength = nil, &block) ⇒ Object
Generates a random phoneme for the current class of at most the given length. In addition, an optional block can be used to determine whether the chosen phoneme is acceptable.
Examples
# Choose any vowel
MemorableStrings::Vowel.random
# => #<MemorableStrings::Vowel:0xb7c3efe4 @first=true, @value="e", @length=1>
# Choose a vowel with at most 1 character
MemorableStrings::Vowel.random(2)
# => <MemorableStrings::Vowel:0xb7c3eb34 @first=true, @value="u", @length=1>
# Choose a vowel that can be the first letter
MemorableStrings::Vowel.random {|vowel| vowel.first?}
# => #<MemorableStrings::Vowel:0xb7c3e080 @first=true, @value="a", @length=1>
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/memorable_strings/phoneme.rb', line 41 def random(maxlength = nil, &block) phonemes = all phonemes = phonemes.select {|phoneme| phoneme.length <= maxlength} if maxlength begin phoneme = phonemes[rand(phonemes.size)] end while !phoneme.matches?(&block) phoneme end |
Instance Method Details
#first? ⇒ Boolean
Is this allowed to be the first in a sequence of phonemes?
102 103 104 |
# File 'lib/memorable_strings/phoneme.rb', line 102 def first? @first end |
#matches? ⇒ Boolean
Does this phoneme match the conditions specified by the block?
112 113 114 |
# File 'lib/memorable_strings/phoneme.rb', line 112 def matches? !block_given? || yield(self) end |
#print_friendly?(context) ⇒ Boolean
Is this character unambiguous with other characters?
107 108 109 |
# File 'lib/memorable_strings/phoneme.rb', line 107 def print_friendly?(context) @print_friendly == true || @print_friendly == context end |