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.
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.
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"
Tested on all major Ruby interpreters (100% coverage, 0% failure):
None: this gem does not depend on any other gem.
sudo gem install heredoc_unindent
After checking out the source, run:
$ rake newb
This task will install any missing dependencies, run the tests/specs, and generate the RDoc.
The initial implementation was based on the answer by Rene Saarsoo to the question how do i remove leading whitespace chars from ruby heredoc.
Despite minor API differences or edge-case behaviour, the following gems implement equivalent unindenting functionality:
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.
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.
(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.