Created
February 14, 2011 21:25
-
-
Save cowboy/826577 to your computer and use it in GitHub Desktop.
Ruby: Unindent arbitrarily-indented heredocs
This file contains hidden or 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
# Ruby: Unindent arbitrarily-indented heredocs | |
# | |
# "Cowboy" Ben Alman | |
# http://benalman.com/ | |
class String | |
def unindent | |
arr = respond_to?(:lines) ? lines : self | |
min = arr.map {|line| line =~ /^(\s*)\S/; $1 && $1.length }.compact.min | |
arr.map {|line| line.sub(/^\s{#{min}}/, '')}.join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See the working example.