Unindent Ruby strings. Handy for heredocs.
Ruby
Permalink
Failed to load latest commit information.
lib
spec Rename from Unindentable to Outdent Jan 5, 2011
.gitignore Version 0.0.4 (uses rspec 2 and bundler) Jan 3, 2011
Gemfile Version 0.0.4 (uses rspec 2 and bundler) Jan 3, 2011
Gemfile.lock Rename from Unindentable to Outdent Jan 5, 2011
LICENSE
README.md Mention ActiveSupport's String#strip_heredoc Jan 28, 2011
outdent.gemspec Corrected gemspec version Jan 28, 2011

README.md

Outdent

With Outdent, 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 Outdent, you can write:

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

And get the nice outdented 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

History

  • 2011-01-05 - Renamed project from Unindentable to Outdent
  • 2011-01-04 - Borrowed (stole?) API from Unindent project
  • 2009-07-09 - First version