Class: Corrector::Words
- Inherits:
-
Array
- Object
- Array
- Corrector::Words
- Defined in:
- app/services/corrector/words.rb
Overview
Breaks source string to words.
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
The source array or string for the current words list.
Instance Method Summary collapse
-
#initialize(value = []) ⇒ Words
constructor
Initializes the array containing words from the source array or string.
-
#map ⇒ Object
Maps
Wordsclass object. -
#to_s ⇒ Object
Converts words list to a string.
Constructor Details
#initialize(value = []) ⇒ Words
Initializes the array containing words from the source array or string.
Params:
value-
An array of words or a string to be splitted to words. Default value:
[].
Returns the array of recognized words from the initial string. Both the map and to_s methods of the array are redefined.
33 34 35 36 |
# File 'app/services/corrector/words.rb', line 33 def initialize(value = []) @source = value value.is_a?(Array) ? super(value) : super(words) end |
Instance Attribute Details
#source ⇒ Object (readonly)
The source array or string for the current words list.
@example:
words = Words.new "М В С2" # => #< Words ["М", "В", "С", "2"]>
words.source # => "М В С2"
words = Wrods.new ["М", "В", "С2"] # => #< Words ["М", "В", "С2"]>
words.source # => ["М", "В", "С2"]
Returns either array or string.
18 19 20 |
# File 'app/services/corrector/words.rb', line 18 def source @source end |
Instance Method Details
#map ⇒ Object
Maps Words class object.
Returns the mapped Words object.
45 46 47 |
# File 'app/services/corrector/words.rb', line 45 def map Words.new super.flatten end |
#to_s ⇒ Object
Converts words list to a string. Joins words divided by the ^ symbol and removes spaces around the slash.
Returns a converted string with source method to check the source.
57 58 59 |
# File 'app/services/corrector/words.rb', line 57 def to_s map(&:strip).join(" ").gsub(/\s*\^\s*/, "").gsub(/\s*\/\s*/, "/") end |