ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
@ 2013-11-09 21:18 sikachu (Prem Sichanugrist)
  2013-11-13  6:21 ` [ruby-core:58305] [ruby-trunk - Feature #9098][Assigned] " drbrain (Eric Hodel)
                   ` (22 more replies)
  0 siblings, 23 replies; 24+ messages in thread
From: sikachu (Prem Sichanugrist) @ 2013-11-09 21:18 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been reported by sikachu (Prem Sichanugrist).

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

Author: sikachu (Prem Sichanugrist)
Status: Open
Priority: Normal
Assignee: sikachu (Prem Sichanugrist)
Category: syntax
Target version: 


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58305] [ruby-trunk - Feature #9098][Assigned] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
@ 2013-11-13  6:21 ` drbrain (Eric Hodel)
  2013-11-13 18:26 ` [ruby-core:58316] [ruby-trunk - Feature #9098] " sikachu (Prem Sichanugrist)
                   ` (21 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: drbrain (Eric Hodel) @ 2013-11-13  6:21 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been updated by drbrain (Eric Hodel).

Status changed from Open to Assigned
Assignee changed from sikachu (Prem Sichanugrist) to matz (Yukihiro Matsumoto)
Target version set to Next Major

Why not make your content flush left?
----------------------------------------
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-42898

Author: sikachu (Prem Sichanugrist)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58316] [ruby-trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
  2013-11-13  6:21 ` [ruby-core:58305] [ruby-trunk - Feature #9098][Assigned] " drbrain (Eric Hodel)
@ 2013-11-13 18:26 ` sikachu (Prem Sichanugrist)
  2013-11-13 23:08 ` [ruby-core:58319] " drbrain (Eric Hodel)
                   ` (20 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: sikachu (Prem Sichanugrist) @ 2013-11-13 18:26 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been updated by sikachu (Prem Sichanugrist).


@drbrain I could, but then the code would look ugly when the non-heredoc section is well-indented, and especially if the heredoc is pretty long. Consider this example code:

    module Rails
      class CommandsTasks # :nodoc:
        attr_reader :argv

        HELP_MESSAGE = <<-EOT
    Usage: rails COMMAND [ARGS]

    The most common rails commands are:
     generate    Generate new code (short-cut alias: "g")
     console     Start the Rails console (short-cut alias: "c")
     server      Start the Rails server (short-cut alias: "s")
     dbconsole   Start a console for the database specified in config/database.yml
                 (short-cut alias: "db")
     new         Create a new Rails application. "rails new my_app" creates a
                 new application called MyApp in "./my_app"

    In addition to those, there are:
     application  Generate the Rails application code
     destroy      Undo code generated with "generate" (short-cut alias: "d")
     plugin new   Generates skeleton for developing a Rails plugin
     runner       Run a piece of code in the application environment (short-cut alias: "r")

    All commands can be run with -h (or --help) for more information.
    EOT

        COMMAND_WHITELIST = %(plugin generate destroy console server dbconsole application runner new version help)

Compared to this:

    module Rails
      class CommandsTasks # :nodoc:
        attr_reader :argv

        HELP_MESSAGE = <<-EOT
          Usage: rails COMMAND [ARGS]

          The most common rails commands are:
           generate    Generate new code (short-cut alias: "g")
           console     Start the Rails console (short-cut alias: "c")
           server      Start the Rails server (short-cut alias: "s")
           dbconsole   Start a console for the database specified in config/database.yml
                       (short-cut alias: "db")
           new         Create a new Rails application. "rails new my_app" creates a
                       new application called MyApp in "./my_app"

          In addition to those, there are:
           application  Generate the Rails application code
           destroy      Undo code generated with "generate" (short-cut alias: "d")
           plugin new   Generates skeleton for developing a Rails plugin
           runner       Run a piece of code in the application environment (short-cut alias: "r")

          All commands can be run with -h (or --help) for more information.
        EOT

        COMMAND_WHITELIST = %(plugin generate destroy console server dbconsole application runner new version help)

From the code example, you could see that it looks much nicer when it's properly indented based on the level. The heredoc doesn't feel foreign and stand out-of-place anymore.
----------------------------------------
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-42911

Author: sikachu (Prem Sichanugrist)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58319] [ruby-trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
  2013-11-13  6:21 ` [ruby-core:58305] [ruby-trunk - Feature #9098][Assigned] " drbrain (Eric Hodel)
  2013-11-13 18:26 ` [ruby-core:58316] [ruby-trunk - Feature #9098] " sikachu (Prem Sichanugrist)
@ 2013-11-13 23:08 ` drbrain (Eric Hodel)
  2013-11-13 23:10 ` [ruby-core:58320] " sikachu (Prem Sichanugrist)
                   ` (19 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: drbrain (Eric Hodel) @ 2013-11-13 23:08 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been updated by drbrain (Eric Hodel).


I can see the second is worse, not better, because it runs over 80 columns on more lines which wrap in my editor.
----------------------------------------
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-42914

Author: sikachu (Prem Sichanugrist)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58320] [ruby-trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (2 preceding siblings ...)
  2013-11-13 23:08 ` [ruby-core:58319] " drbrain (Eric Hodel)
@ 2013-11-13 23:10 ` sikachu (Prem Sichanugrist)
  2013-11-14  1:40 ` [ruby-core:58322] Re: [ruby-trunk - Feature #9098][Open] " Avdi Grimm
                   ` (18 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: sikachu (Prem Sichanugrist) @ 2013-11-13 23:10 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been updated by sikachu (Prem Sichanugrist).


I can see that. I actually just indented the example in without looking at the line width. My bad. :)

If the second example is *not* over 80 columns, would that still be consider worse for you?
----------------------------------------
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-42915

Author: sikachu (Prem Sichanugrist)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58322] Re: [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (3 preceding siblings ...)
  2013-11-13 23:10 ` [ruby-core:58320] " sikachu (Prem Sichanugrist)
@ 2013-11-14  1:40 ` Avdi Grimm
  2013-11-14  1:45 ` [ruby-core:58323] [ruby-trunk - Feature #9098] " drbrain (Eric Hodel)
                   ` (17 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: Avdi Grimm @ 2013-11-14  1:40 UTC (permalink / raw
  To: Ruby developers

[-- Attachment #1: Type: text/plain, Size: 1276 bytes --]

On Sat, Nov 9, 2013 at 4:18 PM, sikachu (Prem Sichanugrist) <s@sikac.hu>
wrote:

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.

 This has actually bugged me for a very long time. Other languages have
triple-quoted strings where the indent of the opening triple-quote is taken
into account, and automatically stripped from the resulting text. In Ruby
if I don’t want leading spaces I have to break the visual flow of my
indentation (or resort to something like #strip_heredoc).

I don’t agree with changing the existing heredocs to have new semantics.
But I could see adding e.g. a "squiggly-heredoc":

class Foo
  BAR = <<~EOF
  This line is
  cleanly indented
  EOFend
Foo::BAR # => "This line is\ncleanly indented"

Note that the way I imagine it, this would work a little different than
triple-quoted strings in other languages, since the indentation wouldn’t be
determined by the location of the opening signifier. Instead, it would be
determined by indentation of the first non-whitespace character inside the
heredoc.
-- 
Avdi Grimm
http://avdi.org

I only check email twice a day. to reach me sooner, go to
http://awayfind.com/avdi

[-- Attachment #2: Type: text/html, Size: 3535 bytes --]

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58323] [ruby-trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (4 preceding siblings ...)
  2013-11-14  1:40 ` [ruby-core:58322] Re: [ruby-trunk - Feature #9098][Open] " Avdi Grimm
@ 2013-11-14  1:45 ` drbrain (Eric Hodel)
  2013-11-14  3:10 ` [ruby-core:58325] " sikachu (Prem Sichanugrist)
                   ` (16 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: drbrain (Eric Hodel) @ 2013-11-14  1:45 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been updated by drbrain (Eric Hodel).


I prefer Avdi's idea in [ruby-core:58320] as it does not introduce incompatibility.
----------------------------------------
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-42917

Author: sikachu (Prem Sichanugrist)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58325] [ruby-trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (5 preceding siblings ...)
  2013-11-14  1:45 ` [ruby-core:58323] [ruby-trunk - Feature #9098] " drbrain (Eric Hodel)
@ 2013-11-14  3:10 ` sikachu (Prem Sichanugrist)
  2013-11-14  4:01 ` [ruby-core:58328] " drbrain (Eric Hodel)
                   ` (15 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: sikachu (Prem Sichanugrist) @ 2013-11-14  3:10 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been updated by sikachu (Prem Sichanugrist).


Oh, that's an awesome suggestion, @avdi. I like it. That way, it wouldn't be introducing backward incompatibility. Maybe we can target next minor (since it's a new feature) as well?

@drbrain, so I think I'll want to change this ticket to be adding `<<~` to do this functionality instead. Should I go ahead and try to implement it, or do I have to wait for @matz to +1?
----------------------------------------
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-42920

Author: sikachu (Prem Sichanugrist)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58328] [ruby-trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (6 preceding siblings ...)
  2013-11-14  3:10 ` [ruby-core:58325] " sikachu (Prem Sichanugrist)
@ 2013-11-14  4:01 ` drbrain (Eric Hodel)
  2013-11-14 10:41 ` [ruby-core:58332] " rosenfeld (Rodrigo Rosenfeld Rosas)
                   ` (14 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: drbrain (Eric Hodel) @ 2013-11-14  4:01 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been updated by drbrain (Eric Hodel).

Target version changed from Next Major to next minor

Creating a patch will help, but it can't be committed without the approval of matz.
----------------------------------------
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-42923

Author: sikachu (Prem Sichanugrist)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: next minor


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58332] [ruby-trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (7 preceding siblings ...)
  2013-11-14  4:01 ` [ruby-core:58328] " drbrain (Eric Hodel)
@ 2013-11-14 10:41 ` rosenfeld (Rodrigo Rosenfeld Rosas)
  2013-11-14 16:07 ` [ruby-core:58340] " sikachu (Prem Sichanugrist)
                   ` (13 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: rosenfeld (Rodrigo Rosenfeld Rosas) @ 2013-11-14 10:41 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


+1 to Avdi's idea. I mostly miss this when I'm writing my specs. Sometimes I have to compare some processed HTML with the expected result and it's not easy to write the expected string without compromising indentation. Usually I end up with something like:

[
  'First line',
  'Second line',
  ...
].join "\n"

Having something like what Avdi described would help me a lot to write some of my specs.
----------------------------------------
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-42928

Author: sikachu (Prem Sichanugrist)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: next minor


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:58340] [ruby-trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (8 preceding siblings ...)
  2013-11-14 10:41 ` [ruby-core:58332] " rosenfeld (Rodrigo Rosenfeld Rosas)
@ 2013-11-14 16:07 ` sikachu (Prem Sichanugrist)
  2015-04-20 11:42 ` [ruby-core:68937] [Ruby trunk " bjmllr
                   ` (12 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: sikachu (Prem Sichanugrist) @ 2013-11-14 16:07 UTC (permalink / raw
  To: ruby-core


Issue #9098 has been updated by sikachu (Prem Sichanugrist).


Sweet. I'll go ahead and start cooking up the patch. Thanks all for the preliminary feedback.
----------------------------------------
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-42939

Author: sikachu (Prem Sichanugrist)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: next minor


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


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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:68937] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (9 preceding siblings ...)
  2013-11-14 16:07 ` [ruby-core:58340] " sikachu (Prem Sichanugrist)
@ 2015-04-20 11:42 ` bjmllr
  2015-11-09  6:36 ` [ruby-core:71394] " ko1
                   ` (11 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: bjmllr @ 2015-04-20 11:42 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Ben Miller.


This feature request has been silent for a year, so I submitted a patch for it: https://github.com/ruby/ruby/pull/878

----------------------------------------
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-52201

* Author: Prem Sichanugrist
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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



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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71394] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (10 preceding siblings ...)
  2015-04-20 11:42 ` [ruby-core:68937] [Ruby trunk " bjmllr
@ 2015-11-09  6:36 ` ko1
  2015-11-09  6:52 ` [ruby-core:71396] " matz
                   ` (10 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: ko1 @ 2015-11-09  6:36 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Koichi Sasada.


Discussion: https://docs.google.com/document/d/1D0Eo5N7NE_unIySOKG9lVj_eyXf66BQPM4PKp7NvMyQ/pub

Feel free to continue discussion on this ticket.


----------------------------------------
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-54763

* Author: Prem Sichanugrist
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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



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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71396] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (11 preceding siblings ...)
  2015-11-09  6:36 ` [ruby-core:71394] " ko1
@ 2015-11-09  6:52 ` matz
  2015-11-09 15:00 ` [ruby-core:71418] " andrewm.bpi
                   ` (9 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: matz @ 2015-11-09  6:52 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Yukihiro Matsumoto.


Accepted. We have to define the behavior on (hard) tabs. As a UNIX user, I'd like to tak 8 spaces for tab stops.

Matz.


----------------------------------------
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-54765

* Author: Prem Sichanugrist
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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



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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71418] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (12 preceding siblings ...)
  2015-11-09  6:52 ` [ruby-core:71396] " matz
@ 2015-11-09 15:00 ` andrewm.bpi
  2015-11-28  1:31 ` [ruby-core:71716] " bjmllr
                   ` (8 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: andrewm.bpi @ 2015-11-09 15:00 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Andrew M.


What do you mean? Isn't the number of spaces a tab is equivalent to only relevant when the first line is indented with tabs, while the following lines are indented with spaces (or vice-versa)?

Shouldn't it just throw an error in that case?

----------------------------------------
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-54787

* Author: Prem Sichanugrist
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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



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

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71716] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (13 preceding siblings ...)
  2015-11-09 15:00 ` [ruby-core:71418] " andrewm.bpi
@ 2015-11-28  1:31 ` bjmllr
  2015-11-28  3:14 ` [ruby-core:71717] " andrewm.bpi
                   ` (7 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: bjmllr @ 2015-11-28  1:31 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Ben Miller.

File dedent_heredoc.patch added

I updated the patch to treat tabs as equal to 8 spaces.

This seems to work fine with the indentation produced by popular text editors (only tabs, only spaces, or tabs-then-spaces), with only one corner case I noticed, which is demonstrated in test_dedented_heredoc_with_inconsistent_indentation_preserves_tab (in test/ruby/test_syntax.rb), in which case we try backing up to the previous tab stop and starting over, to avoid breaking the hard tab. Do we need to handle spaces-then-tabs?

I attached the patch here so that more people are able to see it.

----------------------------------------
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-55122

* Author: Prem Sichanugrist
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71717] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (14 preceding siblings ...)
  2015-11-28  1:31 ` [ruby-core:71716] " bjmllr
@ 2015-11-28  3:14 ` andrewm.bpi
  2015-11-28  3:37 ` [ruby-core:71718] " matz
                   ` (6 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: andrewm.bpi @ 2015-11-28  3:14 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Andrew M.


I'm confused. Why are tabs being treated as equivalent to spaces at all? E.g. If I write:

```ruby
def hello
  puts <<~README.inspect
<tab>Hello

<space><space><space><space><space><space><space><space>World!
    README
  end
end
```

Are you saying that should be accepted by the compiler? Why? Why should that be any less invalid than:

```ruby
def hello
  puts <<~README.inspect
<tab>Hello

<space><space><space><space>World!
    README
  end
end
```

or

```ruby
def hello
  puts <<~README.inspect
<space><space><space><space>Hello

<space><space>World!
    README
  end
end
```

Shouldn't we just throw an error in all of those cases? Is there ever a legitimate reason why you'd want to allow inconsistent indentation in one of these blocks? What happens when someone has their editor set to display tabs as 4 spaces, and writes:

```ruby
def hello
  puts <<~README.inspect
<space><space><space><space>Hello

<tab>World!
    README
  end
end
```

Why should that result in:

```ruby
hello #=> "    Hello\n\n\tWorld!"
```

I certainly wouldn't expect that result intuitively. In such a case, wouldn't a well-written error message explaining that I'm mixing tabs and spaces be much more helpful for me as a developer?

(Note: I also cross-posted to the PR. I'm not really sure where the canonical place is for discussion on this feature.)

----------------------------------------
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-55123

* Author: Prem Sichanugrist
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71718] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (15 preceding siblings ...)
  2015-11-28  3:14 ` [ruby-core:71717] " andrewm.bpi
@ 2015-11-28  3:37 ` matz
  2015-11-28 17:37 ` [ruby-core:71722] " andrewm.bpi
                   ` (5 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: matz @ 2015-11-28  3:37 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Yukihiro Matsumoto.


Because we can not distinguish tabs and spaces on most editors, while we can distinguish 4 spaces and 8 spaces.
It's weird but has historical reason.

Matz.


----------------------------------------
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-55124

* Author: Prem Sichanugrist
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71722] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (16 preceding siblings ...)
  2015-11-28  3:37 ` [ruby-core:71718] " matz
@ 2015-11-28 17:37 ` andrewm.bpi
  2015-12-01  6:20 ` [ruby-core:71773] " nobu
                   ` (4 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: andrewm.bpi @ 2015-11-28 17:37 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Andrew M.


Right. I guess what I'm saying is that tabs shouldn't be treated as equivalent to spaces at all, since the number of spaces they are "equivalent" to is different for every developer, depending on what they have their editor set to display.

This can and will result in unexpected behavior (aka bugs) when tabs and spaces are mixed in indentation by mistake. I'd much rather these bugs be caught by the compiler and brought to the developer's attention with a clearly-worded error message then simply ignored and left for the developer to find out about later.

Tabs should be treated as tabs, and spaces should be treated as spaces. I'm not really sure what the argument is for treating one character as equivalent to another in the first place. Is there any situation where a developer might _want_ that behavior? What's the use-case here? I honestly can't think of one.

To me, this just feels like a way to make it easy for developers to unknowingly create bugs in their code.

----------------------------------------
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-55128

* Author: Prem Sichanugrist
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71773] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (17 preceding siblings ...)
  2015-11-28 17:37 ` [ruby-core:71722] " andrewm.bpi
@ 2015-12-01  6:20 ` nobu
  2015-12-07  6:45 ` [ruby-core:71867] " matz
                   ` (3 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: nobu @ 2015-12-01  6:20 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Nobuyoshi Nakada.


"8 spaces for tab stops" is not enough to make the spec.

When tabs and spaces are mixed, e.g., there are`"\t"*2+"2 tabs"` line and `" "*10+"10 spaces"` line, what should be expected?

1. dedent at the greatest indent column where all lines have a white space.

    a tab which includes the column will remain, but spaces between previous tab stop and the column will be removed.
    this is the implementation by @bjmllr, and results
    `"\t"+"2 tabs"` line and `" "*2+"10 spaces"` line.

2. dedent at the greatest indent column exclusively tabs

    a tab which includes the column will remain, and also spaces before the column will be removed.
    this is like `kill-rectangle` of Emacs, and I've thought this, and results
    `"\t"+"2 tabs"` line and `"10 spaces"` line.

3. expand tabs first

    tabs are expanded first before dedent, and results
    `" "*6+"2 tabs"` line and `"10 spaces"` line.

4. or, like Andrew M claims, dedent of byte-wise common parts can be an option, I think now.

----------------------------------------
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-55183

* Author: Prem Sichanugrist
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71867] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (18 preceding siblings ...)
  2015-12-01  6:20 ` [ruby-core:71773] " nobu
@ 2015-12-07  6:45 ` matz
  2015-12-07 15:19 ` [ruby-core:71909] " andrewm.bpi
                   ` (2 subsequent siblings)
  22 siblings, 0 replies; 24+ messages in thread
From: matz @ 2015-12-07  6:45 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Yukihiro Matsumoto.

Assignee changed from Yukihiro Matsumoto to Nobuyoshi Nakada

Alternative 2 looks good to me.

Matz.


----------------------------------------
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-55268

* Author: Prem Sichanugrist
* Status: Assigned
* 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/

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71909] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (19 preceding siblings ...)
  2015-12-07  6:45 ` [ruby-core:71867] " matz
@ 2015-12-07 15:19 ` andrewm.bpi
  2015-12-08  0:10 ` [ruby-core:71921] " usa
  2015-12-08  3:43 ` [ruby-core:71933] " mame
  22 siblings, 0 replies; 24+ messages in thread
From: andrewm.bpi @ 2015-12-07 15:19 UTC (permalink / raw
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71921] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (20 preceding siblings ...)
  2015-12-07 15:19 ` [ruby-core:71909] " andrewm.bpi
@ 2015-12-08  0:10 ` usa
  2015-12-08  3:43 ` [ruby-core:71933] " mame
  22 siblings, 0 replies; 24+ messages in thread
From: usa @ 2015-12-08  0:10 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Usaku NAKAMURA.


Hey, nobu, write NEWS!!

----------------------------------------
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-55327

* 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/

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [ruby-core:71933] [Ruby trunk - Feature #9098] Indent heredoc against the left margin by default when "indented closing identifier" is turned on.
  2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
                   ` (21 preceding siblings ...)
  2015-12-08  0:10 ` [ruby-core:71921] " usa
@ 2015-12-08  3:43 ` mame
  22 siblings, 0 replies; 24+ messages in thread
From: mame @ 2015-12-08  3:43 UTC (permalink / raw
  To: ruby-core

Issue #9098 has been updated by Yusuke Endoh.


This change actually introduces a syntactic incompatibility.  Is this okay?  I have not seen any real-life use-case that suffers from this incompatibility, though.

~~~
def foo
  42
end
BAR = -10
p foo <<~BAR
~~~

~~~
$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

$ ruby t.rb
21504
~~~

~~~
$ ./miniruby -v
ruby 2.3.0dev (2015-12-08 master 52936) [x86_64-linux]

$ ./miniruby t.rb
t.rb:5: can't find string "BAR" anywhere before EOF
t.rb:5: syntax error, unexpected end-of-input
~~~

Ruby syntax is a chaos.

-- 
Yusuke Endoh <mame@ruby-lang.org>

----------------------------------------
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-55337

* 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/

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2015-12-08  3:12 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-11-09 21:18 [ruby-core:58243] [ruby-trunk - Feature #9098][Open] Indent heredoc against the left margin by default when "indented closing identifier" is turned on sikachu (Prem Sichanugrist)
2013-11-13  6:21 ` [ruby-core:58305] [ruby-trunk - Feature #9098][Assigned] " drbrain (Eric Hodel)
2013-11-13 18:26 ` [ruby-core:58316] [ruby-trunk - Feature #9098] " sikachu (Prem Sichanugrist)
2013-11-13 23:08 ` [ruby-core:58319] " drbrain (Eric Hodel)
2013-11-13 23:10 ` [ruby-core:58320] " sikachu (Prem Sichanugrist)
2013-11-14  1:40 ` [ruby-core:58322] Re: [ruby-trunk - Feature #9098][Open] " Avdi Grimm
2013-11-14  1:45 ` [ruby-core:58323] [ruby-trunk - Feature #9098] " drbrain (Eric Hodel)
2013-11-14  3:10 ` [ruby-core:58325] " sikachu (Prem Sichanugrist)
2013-11-14  4:01 ` [ruby-core:58328] " drbrain (Eric Hodel)
2013-11-14 10:41 ` [ruby-core:58332] " rosenfeld (Rodrigo Rosenfeld Rosas)
2013-11-14 16:07 ` [ruby-core:58340] " sikachu (Prem Sichanugrist)
2015-04-20 11:42 ` [ruby-core:68937] [Ruby trunk " bjmllr
2015-11-09  6:36 ` [ruby-core:71394] " ko1
2015-11-09  6:52 ` [ruby-core:71396] " matz
2015-11-09 15:00 ` [ruby-core:71418] " andrewm.bpi
2015-11-28  1:31 ` [ruby-core:71716] " bjmllr
2015-11-28  3:14 ` [ruby-core:71717] " andrewm.bpi
2015-11-28  3:37 ` [ruby-core:71718] " matz
2015-11-28 17:37 ` [ruby-core:71722] " andrewm.bpi
2015-12-01  6:20 ` [ruby-core:71773] " nobu
2015-12-07  6:45 ` [ruby-core:71867] " matz
2015-12-07 15:19 ` [ruby-core:71909] " andrewm.bpi
2015-12-08  0:10 ` [ruby-core:71921] " usa
2015-12-08  3:43 ` [ruby-core:71933] " mame

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).