Module: StringRay
- Defined in:
- lib/stringray.rb
Defined Under Namespace
Classes: Delimiter, Whitespace, Word
Constant Summary collapse
- VERSION =
1
Class Method Summary collapse
-
.included(klass) ⇒ Object
This overrides String#each with StringRay#each_word, thus allowing us to include
Enumerable
.
Instance Method Summary collapse
-
#each_word(opts = {}, &block) ⇒ Object
More sensible than String#each, this uses
#enumerate
to enumerate on words. -
#enumerate ⇒ Object
Splits a string into words.
Class Method Details
.included(klass) ⇒ Object
This overrides String#each with StringRay#each_word, thus allowing us to include Enumerable
.
120 121 122 123 124 125 126 127 |
# File 'lib/stringray.rb', line 120 def self.included klass klass.class_eval do alias_method :each_at, :each alias_method :each, :each_word include Enumerable end end |
Instance Method Details
#each_word(opts = {}, &block) ⇒ Object
More sensible than String#each, this uses #enumerate
to enumerate on words. Accepts options as a hash, determining whether :whitespace and :delemiters will :attach_before, :standalone, or :attach_after. Default is for both to :attach_before.
39 40 41 42 43 44 45 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/stringray.rb', line 39 def each_word opts = {}, &block {:whitespace => :attach_before, :delemiters => :attach_before}.merge! opts # First, we create a two-dimensional array of words with any whitespace or # delemiters that should attach to them. words = self.enumerate mapped = [] attach_before_next = [] words.each do |item| case item when Delimiter case opts[:delemiters] when :standalone mapped << [item] when :attach_after attach_before_next << item else if attach_before_next.empty? mapped.last << item else attach_before_next << item end end when Whitespace case opts[:whitespace] when :standalone mapped << [item] when :attach_after attach_before_next << item else if attach_before_next.empty? mapped.last << item else attach_before_next << item end end when Word if not attach_before_next.empty? mapped << [attach_before_next, item].flatten attach_before_next = [] else mapped << [item] end end end (mapped.last << attach_before_next).flatten! if not attach_before_next.empty? # Next, we yield each group of (word plus delimiters and whitespace) as a # normal string to the block mapped.each do |arr| yield arr.map{|w|w.to_s}.join end end |
#enumerate ⇒ Object
Splits a string into words. Not using the obvious names (#split
, #words
) because I want compatibility for inclusion into String
.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/stringray.rb', line 6 def enumerate ray = [] self.each_byte do |byte| char = byte.chr if Delimiter::Characters.include? char ray << Delimiter.new(char) elsif Whitespace::Characters.include? char if ray.last.is_a? Whitespace ray.last << char else ray << Whitespace.new(char) end else if ray.last.is_a? Word ray.last << char else ray << Word.new(char) end end end ray end |