Module: Corefines::String::Unindent

Defined in:
lib/corefines/string.rb

Instance Method Summary collapse

Instance Method Details

#strip_heredocString

Alias for #unindent.


498
499
500
501
502
503
504
505
506
507
508
# File 'lib/corefines/string.rb', line 498

module Unindent
  refine ::String do
    def unindent
      leading_space = scan(/^[ \t]*(?=\S)/).min
      indent = leading_space ? leading_space.size : 0
      gsub /^[ \t]{#{indent}}/, ''
    end

    alias_method :strip_heredoc, :unindent
  end
end

#unindentString

Remove excessive indentation. Useful for multi-line strings embeded in already indented code.

Technically, it looks for the least indented line in the whole string (blank lines are ignored) and removes that amount of leading whitespace.

Examples:

<<-EOF.unindent
  Greetings,
    programs!
EOF
=> "Greetings\n  programs"

498
499
500
501
502
503
504
505
506
507
508
# File 'lib/corefines/string.rb', line 498

module Unindent
  refine ::String do
    def unindent
      leading_space = scan(/^[ \t]*(?=\S)/).min
      indent = leading_space ? leading_space.size : 0
      gsub /^[ \t]{#{indent}}/, ''
    end

    alias_method :strip_heredoc, :unindent
  end
end