Removes a leading gutter from all lines in the string. The gutter is defined leading whitespace followed by a single pipe character. This method is very useful with heredocs.
# File lib/loquacious/core_ext/string.rb, line 70 70: def gutter 71: self.dup.gutter! 72: end
Removes a leading gutter from all lines in the string. The gutter is defined leading whitespace followed by a single pipe character. This method is very useful with heredocs.
The string will be altered by this method.
# File lib/loquacious/core_ext/string.rb, line 58 58: def gutter! 59: gsub! /^[\t\f\r ]*\|?/, '' 60: self 61: end
Indent the string by the given number of spaces. Alternately, if a leader string is given it will be used to indent with instead of spaces. Indentation is performed at the beginning of the string and after every newline character.
"foo\nbar".indent( 2 ) #=> " foo\n bar"
# File lib/loquacious/core_ext/string.rb, line 41 41: def indent( leader ) 42: leader = 43: Numeric === leader ? ' ' * leader.to_i : leader.to_s 44: str = self.gsub("\n", "\n"+leader) 45: str.insert(0, leader) 46: str 47: end
Reduce the size of the current string to the given width by removing characters from the middle of the string and replacing them with ellipses. If the width is greater than the length of the string, the string is returned unchanged. If the width is less than the length of the ellipses, then the ellipses are returned.
# File lib/loquacious/core_ext/string.rb, line 13 13: def reduce( width, ellipses = '...') 14: raise ArgumentError, "width cannot be negative: #{width}" if width < 0 15: 16: return self if length <= width 17: 18: remove = length - width + ellipses.length 19: return ellipses.dup if remove >= length 20: 21: left_end = (length + 1 - remove) / 2 22: right_start = left_end + remove 23: 24: left = self[0,left_end] 25: right = self[right_start,length-right_start] 26: 27: left << ellipses << right 28: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.