An add-on to the String class
Add some stuff to the String class to allow easy transformation to Regexp and in-place interpolation.
Return new string with all tabs set to spaces
# File lib/Getopt/Declare.rb, line 51 def expand_tabs( tabstop = 8 ) h = self.dup while h.sub!(/(^|\n)([^\t\n]*)(\t+)/ex) { |f| val = ( tabstop * "#$3".length() - ("#$2".length() % tabstop) ) "#$1#$2" + (" " * val) } end h end
Expand all tabs to spaces
# File lib/Getopt/Declare.rb, line 41 def expand_tabs!( tabstop = 8 ) while self.sub!(/(^|\n)([^\t\n]*)(\t+)/ex) { |f| val = ( tabstop * "#$3".length() - ("#$2".length() % tabstop) ) "#$1#$2" + (" " * val) } end self end
Ideas for String-interpolation stuff courtesy of Hal E. Fulton <hal9000@hypermetrics.com> via ruby-talk
# File lib/Getopt/DelimScanner.rb, line 47 def interpolate( scope ) unless scope.is_a?( Binding ) raise TypeError, "Argument to interpolate must be a Binding, not "\ "a #{scope.class.name}" end # $stderr.puts ">>> Interpolating '#{self}'..." copy = self.gsub( /"/, %\": ) eval( '"' + copy + '"', scope ) end
Generated with the Darkfish Rdoc Generator 2.