Class: Spoonerize::Spoonerism
- Inherits:
-
Object
- Object
- Spoonerize::Spoonerism
- Defined in:
- lib/spoonerize/spoonerism.rb
Overview
The main word-flipper.
Constant Summary collapse
- VOWEL_LETTERS =
Letters that always represent vowel sounds.
"aeio"- CONSONANT_LETTERS =
Letters that always represent consonant sounds.
"bcdfghjklmnprstvwxz"- Y_FOLLOWING_CONSONANTS =
Letters that make an initial “y” act like a vowel sound.
"bcdfghjklmnpqrstvwxz"
Instance Attribute Summary collapse
-
#config ⇒ Spoonerize::Config
readonly
Configuration values for this spoonerism.
-
#words ⇒ Array
readonly
The words originally passed at initialization.
Instance Method Summary collapse
-
#all_excluded_words ⇒ Array
Array of words to exclude by combining two arrays: * Any user-passed words, stored in
config.excluded_words* Any lazy words, if lazy mode is true. -
#enough_flippable_words? ⇒ Boolean
True if there are more than one non-excluded word to flip.
-
#initialize(*words, config: Spoonerize.config, lazy: nil, lazy_words: nil, excluded_words: nil, consonants_only: nil, reverse: nil, logfile_name: nil) ⇒ Spoonerize::Spoonerism
constructor
Initialize instance.
-
#save ⇒ Array
Saves the flipped words to the log file, along with the options.
-
#spoonerize ⇒ Array
Iterates through words array, and maps its elements to the output of flip_words.
-
#to_h ⇒ Hash
Spoonerized results as a joined hash.
-
#to_json ⇒ String
Same as to_h, but as json.
-
#to_s ⇒ String
Spoonerized results as a joined string.
Constructor Details
#initialize(*words, config: Spoonerize.config, lazy: nil, lazy_words: nil, excluded_words: nil, consonants_only: nil, reverse: nil, logfile_name: nil) ⇒ Spoonerize::Spoonerism
Initialize instance.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/spoonerize/spoonerism.rb', line 51 def initialize( *words, config: Spoonerize.config, lazy: nil, lazy_words: nil, excluded_words: nil, consonants_only: nil, reverse: nil, logfile_name: nil ) @words = normalize_words(words).map(&:downcase) @config = config.with(**{ lazy: lazy, lazy_words: lazy_words, excluded_words: excluded_words, consonants_only: consonants_only, reverse: reverse, logfile_name: logfile_name }.reject { |_, value| value.nil? }) end |
Instance Attribute Details
#config ⇒ Spoonerize::Config (readonly)
Configuration values for this spoonerism.
35 36 37 |
# File 'lib/spoonerize/spoonerism.rb', line 35 def config @config end |
#words ⇒ Array (readonly)
The words originally passed at initialization.
29 30 31 |
# File 'lib/spoonerize/spoonerism.rb', line 29 def words @words end |
Instance Method Details
#all_excluded_words ⇒ Array
Array of words to exclude by combining two arrays:
-
Any user-passed words, stored in
config.excluded_words -
Any lazy words, if lazy mode is true
129 130 131 132 133 |
# File 'lib/spoonerize/spoonerism.rb', line 129 def all_excluded_words (config.excluded_words + ( config.lazy ? config.lazy_words : [] )).map(&:downcase) end |
#enough_flippable_words? ⇒ Boolean
True if there are more than one non-excluded word to flip
111 112 113 |
# File 'lib/spoonerize/spoonerism.rb', line 111 def enough_flippable_words? words.each_index.count { |index| flippable?(index) } > 1 end |
#save ⇒ Array
Saves the flipped words to the log file, along with the options
119 120 121 |
# File 'lib/spoonerize/spoonerism.rb', line 119 def save log.write([words.join(" "), to_s, .join(", ")]) end |
#spoonerize ⇒ Array
Iterates through words array, and maps its elements to the output of flip_words.
77 78 79 80 81 |
# File 'lib/spoonerize/spoonerism.rb', line 77 def spoonerize raise "Not enough words to flip" unless enough_flippable_words? words.map.with_index { |word, idx| flip_words(word, idx) } end |
#to_h ⇒ Hash
Spoonerized results as a joined hash.
95 96 97 |
# File 'lib/spoonerize/spoonerism.rb', line 95 def to_h words.zip(spoonerize).to_h end |
#to_json ⇒ String
Same as to_h, but as json.
103 104 105 |
# File 'lib/spoonerize/spoonerism.rb', line 103 def to_json to_h.to_json end |
#to_s ⇒ String
Spoonerized results as a joined string.
87 88 89 |
# File 'lib/spoonerize/spoonerism.rb', line 87 def to_s spoonerize.join(" ") end |