Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created February 14, 2011 21:25
Show Gist options
  • Save cowboy/826577 to your computer and use it in GitHub Desktop.
Save cowboy/826577 to your computer and use it in GitHub Desktop.
Ruby: Unindent arbitrarily-indented heredocs
# 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
@cowboy
Copy link
Author

cowboy commented Feb 14, 2011

See the working example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment