Sha256: 0b0e31da648b7448acbe96afa5571fabd71a05d4d06876d7f984c02547803571
Contents?: true
Size: 1.15 KB
Versions: 7
Compression:
Stored size: 1.15 KB
Contents
class String # Indent left or right by n spaces. # (This used to be called #tab and aliased as #indent.) # # CREDIT: Gavin Sinclair, Trans, Tyler Rick def indent(n, c=' ') if n >= 0 gsub(/^/, c * n) else gsub(/^#{Regexp.escape(c)}{0,#{-n}}/, "") end end # Equivalent to String#indent, but modifies the receiver in place. def indent!(n, c=' ') replace(indent(n,c)) end # Remove excessive indentation. Useful for multi-line strings embeded in # already indented code. # # <<-END.unindent # ohaie # wurld # END # # Outputs ... # # ohaie # wurld # # Instead of ... # # ohaie # wurld # # CREDIT: Noah Gibbs, mynyml def unindent(size=nil) if size indent(-size) else char = ' ' self.scan(/^[\ \t]*\S/) do |m| if size.nil? || m.size < size size = m.size char = m[0...-1] end end size -= 1 indent(-size, char) end end # Equivalent to String#unindent, but modifies the receiver in place. # # CREDIT: mynyml def unindent! self.replace(self.unindent) end end
Version data entries
7 entries across 6 versions & 1 rubygems