1 class StringRay < Array
3 # This is mixed into any class including +StringRay+. It exposes
4 # +#to_stray+ and +#enumerate+ to said class's instances.
12 # @see StringRay.whitespace=
13 # Controls how +#enumerate+ deals with whitespace by default.
15 # @param [Symbol] whitespace How to handle whitespace - :attach_before,
16 # :standalone, or :attach_after
17 def self.whitespace= whitespace
18 @@whitespace = whitespace
22 @@whitespace || StringRay::whitespace
27 # @see StringRay.delemiters=
28 # Controls how +#enumerate+ deals with delemiters by default.
30 # @param [Symbol] delemiters How to handle delemiters - :attach_before,
31 # :standalone, or :attach_after
32 def self.delemiters= delemiters
33 @@delemiters = delemiters
37 @@delemiters || StringRay::delemiters
41 # Splits a string into an array of +StringRay+ container objects (+Word+,
42 # +Whitespace+, and +Delimiter+).
44 # @yield [element] Allows each 'element' of the string to be operated on
45 # after it is processed
46 # @yieldparam [Word, Whitespace, Delimiter] element The last processed
48 # @return [StringRay[Word, Whitespace, Delimiter]] An array of +StringRay+
53 new_element = lambda do |element|
54 yield ray.last if block_given? unless ray.empty?
58 self.scan(/./um) do |char|
59 if Delimiter::Characters.include? char
60 new_element[Delimiter.new(char)]
62 elsif Whitespace::Characters.include? char
63 if ray.last.is_a? Whitespace
66 new_element[Whitespace.new(char)]
70 if ray.last.is_a? Word
73 new_element[Word.new(char)]
87 def enumerate options = {}, &block
88 self.to_stray.enumerate options, &block
92 alias_method :each_word, :enumerate
94 def self.included klass
95 unless RUBY_VERSION <= "1.9"
96 alias_method :each, :enumerate