Skip to content
This repository
Newer
Older
100644 26 lines (25 sloc) 0.666 kb
b422cda2 » fxn
2010-08-30 let String#strip_heredoc handle blank lines even if they are not inde…
1 require 'active_support/core_ext/object/try'
2
475ea14f » fxn
2010-08-29 implements String#strip_heredoc
3 class String
4 # Strips indentation in heredocs.
5 #
6 # For example in
7 #
8 # if options[:usage]
9 # puts <<-USAGE.strip_heredoc
10 # This command does such and such.
11 #
12 # Supported options are:
13 # -h This message
14 # ...
15 # USAGE
16 # end
17 #
18 # the user would see the usage message aligned against the left margin.
19 #
20 # Technically, it looks for the least indented line in the whole string, and removes
21 # that amount of leading whitespace.
22 def strip_heredoc
b422cda2 » fxn
2010-08-30 let String#strip_heredoc handle blank lines even if they are not inde…
23 indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
24 gsub(/^[ \t]{#{indent}}/, '')
475ea14f » fxn
2010-08-29 implements String#strip_heredoc
25 end
b422cda2 » fxn
2010-08-30 let String#strip_heredoc handle blank lines even if they are not inde…
26 end
Something went wrong with that request. Please try again.