class String
Public Instance Methods
/( subpath )
click to toggle source
# File lib/antlr3/test/core-extensions.rb, line 5 def /( subpath ) File.join( self, subpath.to_s ) end
expand_tabs( n=8 )
click to toggle source
Expands tabs to n
spaces. Non-destructive. If n
is 0, then tabs are simply removed. Raises an exception if n
is negative.
Thanks to GGaramuno for a more efficient algorithm. Very nice.
CREDIT: Gavin Sinclair CREDIT: Noah Gibbs CREDIT: GGaramuno
# File lib/antlr3/test/core-extensions.rb, line 102 def expand_tabs( n=8 ) n = n.to_int raise ArgumentError, "n must be >= 0" if n < 0 return gsub( /\t/, "" ) if n == 0 return gsub( /\t/, " " ) if n == 1 str = self.dup while str.gsub!( /^([^\t\n]*)(\t+)/ ) { |f| val = ( n * $2.size - ( $1.size % n ) ) $1 << ( ' ' * val ) } end str end
fixed_indent( n )
click to toggle source
# File lib/antlr3/test/core-extensions.rb, line 63 def fixed_indent( n ) self.outdent( self.level_of_indent ).indent( n ) end
here_flow( chr = '| ' )
click to toggle source
# File lib/antlr3/test/core-extensions.rb, line 20 def here_flow( chr = '| ' ) dup.here_flow!( chr ) end
here_flow!( chr = '| ' )
click to toggle source
# File lib/antlr3/test/core-extensions.rb, line 24 def here_flow!( chr = '| ' ) here_indent!( chr ).gsub!( /\n\s+/,' ' ) return( self ) end
here_indent( chr = '| ' )
click to toggle source
# File lib/antlr3/test/core-extensions.rb, line 9 def here_indent( chr = '| ' ) dup.here_indent!( chr ) end
here_indent!( chr = '| ' )
click to toggle source
# File lib/antlr3/test/core-extensions.rb, line 13 def here_indent!( chr = '| ' ) chr = Regexp.escape( chr ) exp = Regexp.new( "^ *#{ chr }" ) self.gsub!( exp,'' ) return self end
indent( n )
click to toggle source
Indent left or right by n spaces. (This used to be called tab and aliased as indent
.)
CREDIT: Gavin Sinclair CREDIT: Trans
# File lib/antlr3/test/core-extensions.rb, line 35 def indent( n ) if n >= 0 gsub( /^/, ' ' * n ) else gsub( /^ {0,#{ -n }}/, "" ) end end
level_of_indent()
click to toggle source
Returns the shortest length of leading whitespace for all non-blank lines
n = %Q( a = 3 b = 4 ).level_of_indent #=> 2 CREDIT: Kyle Yetter
# File lib/antlr3/test/core-extensions.rb, line 59 def level_of_indent self.scan( /^ *(?=\S)/ ).map { |space| space.length }.min || 0 end
margin( n=0 )
click to toggle source
Provides a margin controlled string.
x = %Q{ | This | is | margin controlled! }.margin NOTE: This may still need a bit of tweaking. CREDIT: Trans
# File lib/antlr3/test/core-extensions.rb, line 80 def margin( n=0 ) #d = /\A.*\n\s*(.)/.match( self )[1] #d = /\A\s*(.)/.match( self)[1] unless d d = ( ( /\A.*\n\s*(.)/.match( self ) ) || ( /\A\s*(.)/.match( self ) ) )[ 1 ] return '' unless d if n == 0 gsub( /\n\s*\Z/,'' ).gsub( /^\s*[#{ d }]/, '' ) else gsub( /\n\s*\Z/,'' ).gsub( /^\s*[#{ d }]/, ' ' * n ) end end
outdent( n )
click to toggle source
Outdent just indents a negative number of spaces.
CREDIT: Noah Gibbs
# File lib/antlr3/test/core-extensions.rb, line 47 def outdent( n ) indent( -n ) end
snakecase()
click to toggle source
The reverse of camelcase
. Makes an underscored of a camelcase string.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
Examples
"SnakeCase".snakecase #=> "snake_case" "Snake-Case".snakecase #=> "snake_case" "SnakeCase::Errors".underscore #=> "snake_case/errors"
# File lib/antlr3/test/core-extensions.rb, line 127 def snakecase gsub( /::/, '/' ). # NOT SO SURE ABOUT THIS -T gsub( /([A-Z]+)([A-Z][a-z])/,'\1_\2' ). gsub( /([a-z\d])([A-Z])/,'\1_\2' ). tr( "-", "_" ). downcase end