Module: StringRay
- Defined in:
- lib/stringray.rb
Defined Under Namespace
Modules: Extendables Classes: Delimiter, Whitespace, Word
Constant Summary collapse
- Version =
3
- @@whitespace =
nil
- @@delemiters =
nil
Class Method Summary collapse
- .delemiters ⇒ Object
-
.delemiters=(delemiters) ⇒ Object
Controls how
#enumerate
deals with delemiters. - .included(klass) ⇒ Object
- .whitespace ⇒ Object
-
.whitespace=(whitespace) ⇒ Object
Controls how
#enumerate
deals with whitespace.
Instance Method Summary collapse
- #Delimiter(delimiter) ⇒ Object
-
#enumerate(options = {}) {|word| ... } ⇒ Array[String]
(also: #each_word)
Enumerates a string, similar to
#to_stray
, but returning an array of plain Strings instead of container objects. -
#to_stray {|element| ... } ⇒ Array[Word, Whitespace, Delimiter]
Splits a string into an array of
StringRay
container objects (Word
,Whitespace
, andDelimiter
). - #Whitespace(whitespace) ⇒ Object
- #Word(word) ⇒ Object
Class Method Details
.delemiters ⇒ Object
33 34 35 |
# File 'lib/stringray.rb', line 33 def self.delemiters @@delemiters ||= :attach_before end |
.delemiters=(delemiters) ⇒ Object
Controls how #enumerate
deals with delemiters.
29 30 31 |
# File 'lib/stringray.rb', line 29 def self.delemiters= delemiters @@delemiters = delemiters end |
.included(klass) ⇒ Object
229 230 231 |
# File 'lib/stringray.rb', line 229 def self.included klass klass.send :extend, Extendables end |
.whitespace ⇒ Object
19 20 21 |
# File 'lib/stringray.rb', line 19 def self.whitespace @@whitespace ||= :attach_before end |
.whitespace=(whitespace) ⇒ Object
Controls how #enumerate
deals with whitespace.
15 16 17 |
# File 'lib/stringray.rb', line 15 def self.whitespace= whitespace @@whitespace = whitespace end |
Instance Method Details
#Delimiter(delimiter) ⇒ Object
192 |
# File 'lib/stringray.rb', line 192 def Delimiter delimiter; Delimiter.new delimiter; end |
#enumerate(options = {}) {|word| ... } ⇒ Array[String] Also known as: each_word
Enumerates a string, similar to #to_stray
, but returning an array of plain Strings instead of container objects.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/stringray.rb', line 97 def enumerate = {}, &block mapped = [] attach_before_next = [] self.to_stray do |element| case element when Delimiter case [:delemiters] || StringRay::delemiters when :standalone mapped << [element] when :attach_after attach_before_next << element else if attach_before_next.empty? if mapped.last mapped.last << element else attach_before_next << element end else attach_before_next << element end end when Whitespace case [:whitespace] || StringRay::whitespace when :standalone mapped << [element] when :attach_after attach_before_next << element else if attach_before_next.empty? if mapped.last mapped.last << element else attach_before_next << element end else attach_before_next << element end end when Word if not attach_before_next.empty? mapped << [attach_before_next, element].flatten attach_before_next = [] else mapped << [element] end end end if not attach_before_next.empty? mapped << [Word.new] unless mapped.last (mapped.last << attach_before_next).flatten! end mapped.map do |arr| string = arr.map{|w|w.to_s}.join yield string if block_given? string end end |
#to_stray {|element| ... } ⇒ Array[Word, Whitespace, Delimiter]
Splits a string into an array of StringRay
container objects (Word
, Whitespace
, and Delimiter
).
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/stringray.rb', line 48 def to_stray ray = [] new_element = lambda do |element| yield ray.last if block_given? unless ray.empty? ray << element end self.scan(/./um) do |char| if Delimiter::Characters.include? char new_element[Delimiter.new(char)] elsif Whitespace::Characters.include? char if ray.last.is_a? Whitespace ray.last << char else new_element[Whitespace.new(char)] end else if ray.last.is_a? Word ray.last << char else new_element[Word.new(char)] end end end if block_given? yield ray.last self else ray end end |
#Whitespace(whitespace) ⇒ Object
178 |
# File 'lib/stringray.rb', line 178 def Whitespace whitespace; Whitespace.new whitespace; end |