Class: String
- Defined in:
- lib/modalsupport/string.rb,
lib/modalsupport/string.rb
Instance Method Summary collapse
-
#gsub_each(pattern) ⇒ Object
For each substitution, pass the match data to a block that should return substitution value.
-
#gsub_each!(pattern) ⇒ Object
Mutable version of gsub_each.
-
#match_all(re, i = 0) ⇒ Object
Pass each match to a block: string.match_all{|match_data| …}; returns array of block results.
-
#match_one(re, i = 0) ⇒ Object
Pass the first match to a block: string.match_one{|match_data| …}; returns the block result Similar to Ruby 1.9 match, but does not set the special varialbes $~, $1, etc.
-
#unindent(indent = nil) ⇒ Object
Remove indentation of a string (or replace by the indentation defined by the parameters).
-
#unwrap(wrapping) ⇒ Object
Remove wrapping characters and external whitespace.
Instance Method Details
#gsub_each(pattern) ⇒ Object
For each substitution, pass the match data to a block that should return substitution value
27 28 29 |
# File 'lib/modalsupport/string.rb', line 27 def gsub_each(pattern) gsub(pattern){|str| yield(Regexp.last_match)} end |
#gsub_each!(pattern) ⇒ Object
Mutable version of gsub_each
32 33 34 |
# File 'lib/modalsupport/string.rb', line 32 def gsub_each!(pattern) gsub!(pattern){|str| yield(Regexp.last_match)} end |
#match_all(re, i = 0) ⇒ Object
Pass each match to a block: string.match_all{|match_data| …}; returns array of block results
4 5 6 7 8 9 10 11 12 |
# File 'lib/modalsupport/string.rb', line 4 def match_all(re, i=0) result = [] while r=self.index(re, i) match = Regexp.last_match i = r + match.to_s.length result << yield(match) end result end |
#match_one(re, i = 0) ⇒ Object
Pass the first match to a block: string.match_one{|match_data| …}; returns the block result Similar to Ruby 1.9 match, but does not set the special varialbes $~, $1, etc. in the caller’s space.
16 17 18 19 20 21 22 23 24 |
# File 'lib/modalsupport/string.rb', line 16 def match_one(re, i=0) str = i>0 ? self[i..-1] : self m = str.match(re) if m && block_given? yield m else m end end |
#unindent(indent = nil) ⇒ Object
Remove indentation of a string (or replace by the indentation defined by the parameters)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/modalsupport/string.rb', line 43 def unindent(indent=nil) txt = self.gsub(/\t/, ' '*8) mx = txt.scan(/^ *[^\n\r]/).flatten.map{|s| s[-1,1]==' ' ? nil : (s.size-1)}.compact.min if mx && mx>0 re = Regexp.new('^ {1,'+mx.to_s+"}") txt.gsub!(/^ {1,#{mx}}/, "") end lines = txt.split(/\r?\n/) if lines.first.strip.empty? || lines.last.strip.empty? lines.shift while lines.first.strip.empty? lines.pop while lines.last.strip.empty? end if indent indent = " "*indent if indent.kind_of?(Numeric) lines = lines.map{|line| "#{indent}#{line}"} end lines.join("\n") end |
#unwrap(wrapping) ⇒ Object
Remove wrapping characters and external whitespace
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/modalsupport/string.rb', line 63 def unwrap(wrapping) if wrapping.length==1 open = close = wrapping else open = wrapping[0,wrapping.length/2] close = wrapping[-wrapping.length/2..-1] end if self.match(/\A\s*#{Regexp.escape(open)}(.*)#{Regexp.escape(close)}\s*\Z/m) $1 else self end end |