0.01
No commit activity in last 3 years
No release in over 3 years
This gem removes common margin from indented strings, such as the ones produced by indented heredocs. In other words, it strips out leading whitespace chars at the beggining of each line, but only as much as the line with the smallest margin. It is acknowledged that many strings defined by heredocs are just code and fact is that most parsers are insensitive to indentation. If, however, the strings are to be used otherwise, be it for printing or testing, the extra indentation will probably be an issue and hence this gem.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.13
~> 4.0
 Project Readme

heredoc_unindent¶ ↑

DESCRIPTION:¶ ↑

This gem removes common margin from indented strings, such as the ones produced by indented heredocs. In other words, it strips out leading whitespace chars at the beggining of each line, but only as much as the line with the smallest margin.

It is acknowledged that many strings defined by heredocs are just code and fact is that most parsers are insensitive to indentation. If, however, the strings are to be used otherwise, be it for printing or testing, the extra indentation will probably be an issue and hence this gem.

UPDATE¶ ↑

Since version 2.3, Ruby has squiggly heredoc (<<~), so if your project does not need to support previous Ruby versions, there is no need to install a gem to solve the unindentation problem.

References:

SYNOPSIS:¶ ↑

if true
  s = <<-EOS
    How sad it is
      to be unable
    to unindent heredocs
  EOS
  t = <<-EOS.unindent
    How wonderful it is
      to be able
    to do it!
  EOS
end

s[0..12]  #=>  "      How sad"
t[0..12]  #=>  "How wonderful"

s = "   foo"  #=>  "   foo"
s.unindent!   #=>  "foo"
s.unindent!   #=>  nil
s             #=>  "foo"

u = "  1\n \n\n"
u.unindent == u #=> true
u.unindent(ignore_empty: true) #=> " 1\n\n\n"
u.unindent(ignore_blank: true) #=> "1\n\n\n"

FEATURES:¶ ↑

  • Tested on all major Ruby interpreters (100% coverage, 0% failure):

    • ruby-1.9.2-p136

    • ruby-1.8.7-p330

    • ree-1.8.7-2010.02

    • jruby-1.5.6

    • rbx-1.2.0-20101221

REQUIREMENTS:¶ ↑

  • None: this gem does not depend on any other gem.

INSTALL:¶ ↑

  • sudo gem install heredoc_unindent

DEVELOPERS:¶ ↑

After checking out the source, run:

$ rake newb

This task will install any missing dependencies, run the tests/specs, and generate the RDoc.

ACKNOWLEDGEMENTS¶ ↑

The initial implementation was based on the answer by Rene Saarsoo to the question how do i remove leading whitespace chars from ruby heredoc.

ALTERNATIVES¶ ↑

Despite minor API differences or edge-case behaviour, the following gems implement equivalent unindenting functionality:

  1. outdent (formerly unindentable)

  2. unindent

  3. indentation – this gem has a broader scope, in addition to String#reset_indentation

So why another one?¶ ↑

Well, the fact is that the author only found about the other gems after he had already implemented the initial version of heredoc_unindent. And it was for the good, as benchmarking revealed that heredoc_unindent is by far the most efficient of them, both memory and processor-wise.

Efficiency!¶ ↑

Depending on the Ruby implementation, heredoc_unindent is is about 1.6–2.4x faster than unindent, 1.9–6.6x faster than indentation, and 4–19x faster than outdent. In addition, heredoc_unindent has the lowest memory consumption, requiring only between a fifth and a sixth of the competitors.

LICENSE:¶ ↑

(The MIT License)

Copyright © 2011 Adriano Mitre

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.