Class: Spoonerize::Spoonerism
- Inherits:
-
Object
- Object
- Spoonerize::Spoonerism
- Defined in:
- lib/spoonerize/spoonerism.rb
Overview
The main word-flipper.
Constant Summary collapse
- VOWEL_LETTERS =
"aeio"- CONSONANT_LETTERS =
"bcdfghjklmnprstvwxz"- Y_FOLLOWING_CONSONANTS =
"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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/spoonerize/spoonerism.rb', line 37 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.
21 22 23 |
# File 'lib/spoonerize/spoonerism.rb', line 21 def config @config end |
#words ⇒ Array (readonly)
The words originally passed at initialization.
15 16 17 |
# File 'lib/spoonerize/spoonerism.rb', line 15 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
115 116 117 118 119 |
# File 'lib/spoonerize/spoonerism.rb', line 115 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
97 98 99 |
# File 'lib/spoonerize/spoonerism.rb', line 97 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
105 106 107 |
# File 'lib/spoonerize/spoonerism.rb', line 105 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.
63 64 65 66 67 |
# File 'lib/spoonerize/spoonerism.rb', line 63 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.
81 82 83 |
# File 'lib/spoonerize/spoonerism.rb', line 81 def to_h words.zip(spoonerize).to_h end |
#to_json ⇒ String
Same as to_h, but as json.
89 90 91 |
# File 'lib/spoonerize/spoonerism.rb', line 89 def to_json to_h.to_json end |
#to_s ⇒ String
Spoonerized results as a joined string.
73 74 75 |
# File 'lib/spoonerize/spoonerism.rb', line 73 def to_s spoonerize.join(" ") end |