Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/antlr3/test/core-extensions.rb
Instance Method Summary collapse
- #/(subpath) ⇒ Object
-
#expand_tabs(n = 8) ⇒ Object
Expands tabs to
n
spaces. - #fixed_indent(n) ⇒ Object
- #here_flow(chr = '| ') ⇒ Object
- #here_flow!(chr = '| ') ⇒ Object
- #here_indent(chr = '| ') ⇒ Object
- #here_indent!(chr = '| ') ⇒ Object
-
#indent(n) ⇒ Object
Indent left or right by n spaces.
-
#level_of_indent ⇒ Object
Returns the shortest length of leading whitespace for all non-blank lines.
-
#margin(n = 0) ⇒ Object
Provides a margin controlled string.
-
#outdent(n) ⇒ Object
Outdent just indents a negative number of spaces.
-
#snakecase ⇒ Object
The reverse of
camelcase
.
Instance Method Details
#/(subpath) ⇒ Object
5 6 7 |
# File 'lib/antlr3/test/core-extensions.rb', line 5 def /( subpath ) File.join( self, subpath.to_s ) end |
#expand_tabs(n = 8) ⇒ Object
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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/antlr3/test/core-extensions.rb', line 102 def ( 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) ⇒ Object
63 64 65 |
# 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 = '| ') ⇒ Object
20 21 22 |
# File 'lib/antlr3/test/core-extensions.rb', line 20 def here_flow( chr = '| ' ) dup.here_flow!( chr ) end |
#here_flow!(chr = '| ') ⇒ Object
24 25 26 27 |
# 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 = '| ') ⇒ Object
9 10 11 |
# File 'lib/antlr3/test/core-extensions.rb', line 9 def here_indent( chr = '| ' ) dup.here_indent!( chr ) end |
#here_indent!(chr = '| ') ⇒ Object
13 14 15 16 17 18 |
# 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) ⇒ Object
Indent left or right by n spaces. (This used to be called #tab and aliased as #indent.)
CREDIT: Gavin Sinclair
CREDIT: Trans
35 36 37 38 39 40 41 |
# 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 ⇒ Object
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
59 60 61 |
# 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) ⇒ Object
Provides a margin controlled string.
x = %Q{
| This
| is
| margin controlled!
}.margin
NOTE: This may still need a bit of tweaking.
CREDIT: Trans
80 81 82 83 84 85 86 87 88 89 90 91 |
# 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) ⇒ Object
Outdent just indents a negative number of spaces.
CREDIT: Noah Gibbs
47 48 49 |
# File 'lib/antlr3/test/core-extensions.rb', line 47 def outdent( n ) indent( -n ) end |
#snakecase ⇒ Object
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"
127 128 129 130 131 132 133 |
# 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 |