Created
June 25, 2011 03:48
-
-
Save sj26/1046117 to your computer and use it in GitHub Desktop.
String dedent/redent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def dedent | |
indent = lines.reject(&:empty?).map { |line| line.index(/\S/) }.compact.min | |
gsub /^ {1,#{indent.to_i}}/, '' | |
end unless method_defined? :dedent | |
def redent prefix | |
prefix = " " * prefix if prefix.is_a? Numeric | |
dedent.gsub! /^(?=[ \t]*\S+)/, prefix | |
end unless method_defined? :redent | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment