Class: Spoonerize::Spoonerism
- Inherits:
-
Object
- Object
- Spoonerize::Spoonerism
- Defined in:
- lib/spoonerize/spoonerism.rb
Overview
The main word-flipper.
Instance Attribute Summary collapse
-
#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
Spoonerize.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) ⇒ 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) ⇒ Spoonerize::Spoonerism
Initialize instance.
19 20 21 |
# File 'lib/spoonerize/spoonerism.rb', line 19 def initialize(words) @words = words.map(&:downcase) end |
Instance Attribute Details
#words ⇒ Array (readonly)
The words originally passed at initialization.
11 12 13 |
# File 'lib/spoonerize/spoonerism.rb', line 11 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
Spoonerize.config.excluded_words -
Any lazy words, if lazy mode is true
80 81 82 83 84 |
# File 'lib/spoonerize/spoonerism.rb', line 80 def all_excluded_words (Spoonerize.config.excluded_words + ( Spoonerize.config.lazy ? Spoonerize.config.lazy_words : [] )).map(&:downcase) end |
#enough_flippable_words? ⇒ Boolean
True if there are more than one non-excluded word to flip
62 63 64 |
# File 'lib/spoonerize/spoonerism.rb', line 62 def enough_flippable_words? (words - all_excluded_words).size > 1 end |
#save ⇒ Array
Saves the flipped words to the log file, along with the options
70 71 72 |
# File 'lib/spoonerize/spoonerism.rb', line 70 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.
28 29 30 31 32 |
# File 'lib/spoonerize/spoonerism.rb', line 28 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.
46 47 48 |
# File 'lib/spoonerize/spoonerism.rb', line 46 def to_h words.zip(spoonerize).to_h end |
#to_json ⇒ String
Same as to_h, but as json.
54 55 56 |
# File 'lib/spoonerize/spoonerism.rb', line 54 def to_json to_h.to_json end |
#to_s ⇒ String
Spoonerized results as a joined string.
38 39 40 |
# File 'lib/spoonerize/spoonerism.rb', line 38 def to_s spoonerize.join(" ") end |