Project

General

Profile

Actions

Feature #19428

open

Adding a "piped heredoc" feature

Added by shreeve (Steve Shreeve) almost 2 years ago. Updated almost 2 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:112313]

Description

Hello,

I hope this is the correct place to post a small feature request.

HEREDOC's are awesome! There are several used within Ruby:

  <<END
Have to left justify this
END

  <<-INDENTED
Still left justified, but indented ending
  INDENTED

  <<~SQUIGGLY
    I can indent content it will be smartly "dedented/undented". Looks nicer.
  SQUIGGLY

I love using the SQUIGGLY heredoc, which will strip off the minimum whitespace from the front of each line in the heredoc. I often do something like this:

options = {
  name: "My Nice Options",
  description: <<~END
    This is a cool set of options.
    You Can put what you like here.
  END
}

If you need to continue with the above, you can add a comma after like this:

options = {
  name: "My Nice Options",
  description: <<~END,
    This is a cool set of options.
    You Can put what you like here.
  END
  details: <<~END,
    Some more stuff here...
      This will only be indented 2 spaces.
        And this 4 spaces.
      And this 2 spaces again.
    Back to the "left" side.
  END
}

You can even get a little squirrely and use an empty string for a heredoc terminator:

options = {
  name: "My Nice Options",
  description: <<~"",
    This is a cool set of options.
    You Can put what you like here.
  
  details: <<~"",
    Some more stuff here...
      This will only be indented 2 spaces.
        And this 4 spaces.
      And this 2 spaces again.
    Back to the "left" side.

}

There's one variation that I think would be nice to add, I call it a "piped heredoc". It would essentially work like the squiggly heredoc, but the "terminator" would be the next line that is not indented, and thus not "part of the heredoc".

Here's an example:

options = {
  name: "My Nice Options",
  description: <<|,
    This is a cool set of options.
    You Can put what you like here.
  details: <<|,
    Some more stuff here...
      This will only be indented 2 spaces.
        And this 4 spaces.
      And this 2 spaces again.
    Back to the "left" side.
}

Since the leading whitespace is used to tell when the heredoc ends, there is no need for an explicit terminator.

You could add one if you'd like to indicate the flavor of the content, for syntax highlighters, but it's value is ignored:

options = {
  name: "My Nice Options",
  description: <<|RUBY,
    users.each do |user|
      user.pay_daily_bonus!
    end
  details: <<|,
    Some more stuff here...
      This will only be indented 2 spaces.
        And this 4 spaces.
      And this 2 spaces again.
    Back to the "left" side.
}

In the above, the "RUBY" is not really needed, but it is a hint to format that block as Ruby code.

This tweak to Ruby's heredocs seems like it could make Ruby syntax easy and fun to read.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0