Issue #9098 has been updated by Andrew M.


Ah, for some reason I was under the impression that the plan was to use the indentation from the first non-blank line to determine how much to dedent the entire string.

If the plan instead is to use the least-indented non-blank line for that, then the other options make much more sense IMO since there's no way to determine definitively whether leading indention was intended to be removed by Ruby vs used in the string. I actually like this way better come to think of it, since it makes it possible to indent the first line in the string, whereas with the method I was envisioning that would be impossible.

For the record, I'm still in favor of option 4 (or rather, some combination of 4 and 2 which throws an error when their behaviors differ) as I think it's the least likely to result in unexpected behavior for developers, but if Matz has made up his mind I guess there's not much I can do about it. I suppose I'll just have to add a rule to my linter to help prevent those kinds of mistakes.

The results from methods 1, 2, and 4 should always be the same in practice. If they're not, it's almost certainly a case of the developer doing something they didn't intend (a bug).

----------------------------------------
Feature #9098: Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
https://bugs.ruby-lang.org/issues/9098#change-55314

* Author: Prem Sichanugrist
* Status: Closed
* Priority: Normal
* Assignee: Nobuyoshi Nakada
----------------------------------------
tl;dr: I would like to port +String#strip_heredoc+ (http://api.rubyonrails.org/classes/String.html#method-i-strip_heredoc) from Rails and enable it on `<<-` heredoc.

Hi,

I've been using here document (heredoc) for a while, and I've found out that it's very annoying that the content is always treated as flushed left. Per syntax doc:

    Note that the while the closing identifier may be indented, the content is
    always treated as if it is flush left.  If you indent the content those spaces
    will appear in the output.

So, this is the current result if you use heredoc in Ruby:

    class FancyHello
      def self.hello
        puts <<-README.inspect
          Hello
            World!
        README
      end
    end
    
    FancyHello.hello # => "      Hello\n        World!\n"

In Rails, the core team has implemented +String#strip_heredoc+, which handles this scenario by remove leading white spaces  from the string. However, not many user know that Rails has that method, and instead doing this to get around it:

    class FancyHello
      def self.hello
        puts <<-README.inspect
    Hello
      World!
        README
      end
    end
    
    FancyHello.hello # => "Hello\n  World!\n"

I really think that we could do better on this by removing the leading white spaces, matching +String#strip_heredoc+ method.

So, after the change, I want to be able to do this, and get this result:

    class FancyHello
      def self.hello
        puts <<-README.inspect
          Hello
            World!
        README
      end
    end
    
    FancyHello.hello # => "Hello\n  World!\n"

Note: the behavior on `<<HEREDOC` will stay the same, as I feel like in that case you really want the input to be flushed left.

I'll write up a patch and submit it if you think this is a good idea. Please let me know any question or concern you may have.

Thank you,

Prem

---Files--------------------------------
dedent_heredoc.patch (12.8 KB)


-- 
https://bugs.ruby-lang.org/