0.01
No commit activity in last 3 years
No release in over 3 years
Unindent strings, especially heredocs.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.4
 Project Readme

Unindentable

With Unindentable, you can write heredocs without worry of wonky indentation.

Motivation

Heredocs are convenient. But the indentation can bite you. For example:

def hello
  html = <<-BLOCK
    <html>
      <body>
        <p>Hello</p>
      </body>
    </html>
  BLOCK
end

The problem is that you get extra indentation you probably don't want:

irb> hello
=> "    <html>\n      <body>\n        <p>Hello</p>\n      </body>\n    </html>\n"

One solution is to move your heredoc to the left margin:

def hello
  html = <<-BLOCK
<html>
  <body>
    <p>Hello</p>
  </body>
</html>
  BLOCK
end

So that you get the result you expect:

> hello
=> "<html>\n  <body>\n    <p>Hello</p>\n  </body>\n</html>\n"

But this doesn't look good in your source. With Unindentable, you can write:

def hello
  html = <<-BLOCK.unindent
    <html>
      <body>
        <p>Hello</p>
      </body>
    </html>
  BLOCK
end

And get the nice unindented result you want:

> hello
=> "<html>\n  <body>\n    <p>Hello</p>\n  </body>\n</html>\n"

That's it.

The Backstory

Here are some examples from the Interwebs where people discuss potential solutions:

Alternatives