From: "shreeve (Steve Shreeve) via ruby-core" <ruby-core@...>
Date: 2023-02-20T18:12:33+00:00
Subject: [ruby-core:112516] [Ruby master Feature#19428] Adding a "piped heredoc" feature

Issue #19428 has been updated by shreeve (Steve Shreeve).


@nobu - I'll see if I can implement a patch for this. Your steps are helpful.

@eregon - I also think the pipe character prefixed lines are hard to maintain. There are many whitespace aligned things in the wild, such as YAML, python, and many others. This opt-in syntax saves the need for the "obligatory wasted final line" of the other heredoc formats. The final terminator becomes like a positive lookahead regex, which has an implicit ending, and is very helpful in several use cases.

----------------------------------------
Feature #19428: Adding a "piped heredoc" feature
https://bugs.ruby-lang.org/issues/19428#change-101961

* Author: shreeve (Steve Shreeve)
* Status: Open
* Priority: Normal
----------------------------------------

Hello,

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

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

```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:

```ruby
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:

```ruby
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:

```ruby
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:

```ruby
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:

```ruby
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.




-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/