class String
Monkey patch string with some useful methods
Public Instance Methods
erb(vars = {})
click to toggle source
Easily inject ERB variables into a string @param vars [Hash] of variables to inject into the string
# File lib/nub/core.rb, line 153 def erb(vars = {}) ERBResolve.new(vars).resolve(self) end
erb!(vars = {})
click to toggle source
Easily inject ERB variables into a string @param vars [Hash] of variables to inject into the string
# File lib/nub/core.rb, line 159 def erb!(vars = {}) ERBResolve.new(vars).resolve!(self) end
strip_color()
click to toggle source
Strip the ansi color codes from the given string @returns [String] string without any ansi codes
# File lib/nub/core.rb, line 176 def strip_color return self.gsub(/\e\[0;[39]\d;49m/, '').gsub(/\e\[0m/, '') end
to_ascii()
click to toggle source
Convert the string to ascii, stripping out or converting all non-ascii characters
# File lib/nub/core.rb, line 164 def to_ascii options = { :invalid => :replace, :undef => :replace, :replace => '', :universal_newline => true } return self.encode(Encoding.find('ASCII'), options) end
tokenize_color()
click to toggle source
Tokenize the given colorized string @returns [Array] array of Token
# File lib/nub/core.rb, line 182 def tokenize_color tokens = [] matches = self.to_enum(:scan, /\e\[0;[39]\d;49m(.*?[\s]*)\e\[0m/).map{Regexp.last_match} i, istart, iend = 0, 0, 0 match = matches[i] while istart < self.size color = "39" iend = self.size token = self[istart..iend] # Current token is not a match if match && match.begin(0) != istart iend = match.begin(0)-1 token = self[istart..iend] istart = iend + 1 # Current token is a match elsif match && match.begin(0) == istart iend = match.end(0) token = match.captures.first color = match.to_s[/\e\[0;(\d+);49m.*/, 1] i += 1; match = matches[i] istart = iend # Ending else istart = iend end # Create token and advance tokens << ColorPair.new(token, color.to_i, ColorMap[color.to_i]) end return tokens end