Module: Tins::To

Included in:
Object
Defined in:
lib/tins/to.rb

Overview

Provides a simple way to remove common leading whitespace from multi-line strings, mostly for to(<<-EOT), if require “tins/xt/to”. Today you would probably use <<~EOT in this case.

Example usage:

doc = to(<<-EOT)
  hello
    world
  end
EOT
# => "hello\n  world\nend"

Instance Method Summary collapse

Instance Method Details

#to(string) ⇒ String

Remove common leading whitespace from multi-line strings

Parameters:

  • string (String)

    The multi-line string to deindent

Returns:

  • (String)

    String with common leading whitespace removed



18
19
20
21
# File 'lib/tins/to.rb', line 18

def to(string)
  shift_width = (string[/\A\s*/]).size
  string.gsub(/^[^\S\n]{0,#{shift_width}}/, '')
end