class Spoonerize::Spoonerism
The main word-flipper.
Constants
- CONSONANT_LETTERS
-
Letters that always represent consonant sounds.
@return [String]
- VOWEL_LETTERS
-
Letters that always represent vowel sounds.
@return [String]
- Y_FOLLOWING_CONSONANTS
-
Letters that make an initial “y” act like a vowel sound.
@return [String]
Attributes
Configuration values for this spoonerism.
@return [Spoonerize::Config]
The words originally passed at initialization.
@return [Array]
Public Class Methods
Source
# 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
Initialize instance.
@param [Array<String>] words Words to spoonerize. Passing a single array
is deprecated and will be removed in Spoonerize 1.0.
@param [Spoonerize::Config] config Base config to copy. @param [Boolean, nil] lazy Override lazy mode for this instance. @param [Array<String>, nil] lazy_words Override lazy words for this instance. @param [Array<String>, nil] excluded_words Override excluded words for this instance. @param [Boolean, nil] consonants_only Override consonants-only mode for this instance. @param [Boolean, nil] reverse Override reverse mode for this instance. @param [String, nil] logfile_name Override the log file path for this instance.
@return [Spoonerize::Spoonerism]
Public Instance Methods
Source
# File lib/spoonerize/spoonerism.rb, line 129 def all_excluded_words (config.excluded_words + ( config.lazy ? config.lazy_words : [] )).map(&:downcase) end
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
@return [Array]
Source
# File lib/spoonerize/spoonerism.rb, line 111 def enough_flippable_words? words.each_index.count { |index| flippable?(index) } > 1 end
True if there are more than one non-excluded word to flip
@return [Boolean]
Source
# File lib/spoonerize/spoonerism.rb, line 119 def save log.write([words.join(" "), to_s, options.join(", ")]) end
Saves the flipped words to the log file, along with the options
@return [Array]
Source
# 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
Iterates through words array, and maps its elements to the output of flip_words.
@return [Array]
Source
# File lib/spoonerize/spoonerism.rb, line 95 def to_h words.zip(spoonerize).to_h end
Spoonerized results as a joined hash.
@return [Hash]
Source
# File lib/spoonerize/spoonerism.rb, line 103 def to_json to_h.to_json end
Same as to_h, but as json.
@return [String]
Source
# File lib/spoonerize/spoonerism.rb, line 87 def to_s spoonerize.join(" ") end
Spoonerized results as a joined string.
@return [String]