Skip to content

Instantly share code, notes, and snippets.

@sj26
Created June 25, 2011 03:48
Show Gist options
  • Save sj26/1046117 to your computer and use it in GitHub Desktop.
Save sj26/1046117 to your computer and use it in GitHub Desktop.
String dedent/redent
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