Module: StringRay
- Defined in:
- lib/stringray.rb
Defined Under Namespace
Modules: Extendables Classes: Delimiter, Whitespace, Word
Constant Summary collapse
- VERSION =
2
- @@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
31 32 33 |
# File 'lib/stringray.rb', line 31 def self.delemiters @@delemiters ||= :attach_before end |
.delemiters=(delemiters) ⇒ Object
Controls how #enumerate
deals with delemiters.
27 28 29 |
# File 'lib/stringray.rb', line 27 def self.delemiters= delemiters @@delemiters = delemiters end |
.included(klass) ⇒ Object
224 225 226 |
# File 'lib/stringray.rb', line 224 def self.included klass klass.send :extend, Extendables end |
.whitespace ⇒ Object
17 18 19 |
# File 'lib/stringray.rb', line 17 def self.whitespace @@whitespace ||= :attach_before end |
.whitespace=(whitespace) ⇒ Object
Controls how #enumerate
deals with whitespace.
13 14 15 |
# File 'lib/stringray.rb', line 13 def self.whitespace= whitespace @@whitespace = whitespace end |
Instance Method Details
#Delimiter(delimiter) ⇒ Object
190 |
# File 'lib/stringray.rb', line 190 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.
95 96 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 |
# File 'lib/stringray.rb', line 95 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
).
46 47 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 |
# File 'lib/stringray.rb', line 46 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
176 |
# File 'lib/stringray.rb', line 176 def Whitespace whitespace; Whitespace.new whitespace; end |