[ruby-core:119796] [Ruby master Feature#20852] Anonymous HEREDOC blocks
From:
"matz (Yukihiro Matsumoto) via ruby-core" <ruby-core@...>
Date:
2024-11-07 09:45:47 UTC
List:
ruby-core #119796
Issue #20852 has been updated by matz (Yukihiro Matsumoto).
Status changed from Open to Rejected
I don't think we should enhance here-doc syntax any further. It's too complex already.
Matz.
----------------------------------------
Feature #20852: Anonymous HEREDOC blocks
https://bugs.ruby-lang.org/issues/20852#change-110481
* Author: bradgessler (Brad Gessler)
* Status: Rejected
----------------------------------------
Consider the following code.
```ruby
# Consider the following code.
Markdown.render <<~MARKDOWN
# Hello there
This is a Markdown file. See?
1. This is a list
2. With items
3. And more items
MARKDOWN
```
There's a lot of "Markdown" that's repeated.
```ruby
# Or this
SQL.execute <<~SQL
SELECT * FROM users WHERE id = 1;
SQL
```
It's fine. It works. The key feature is that it understands the text in that block is indented.
Not requiring the name could make the syntax less redundant and free developers up from having to name the string. Here's what that could look like:
```ruby
# Now with 75% less markdown!
Markdown.render <<~
# Hello there
This is a Markdown file. See?
1. This is a list
2. With items
3. And more items
~>>
# We can't sell SQL symbols fast enough!
SQL.execute <<~
SELECT * FROM users WHERE id = 1;
~>>
```
I implement a lot of my application UI in Phlex. The dream is that I can build pages out that look like this:
```ruby
class Form < ApplicationForm
def view_template
input(type: "text", name: "name", placeholder: "Name")
fieldset{
h2 { "Agreement" }
article(class: "prose"){
markdown <<~
## Terms and Conditions
By submitting this form, you agree to the terms and conditions of this website.
# Privacy Policy
We will not share your information with anyone.
...
~>>
}
}
button{ "Submit" }
end
end
```
The closest I can get to that today is if I do this:
```ruby
SQL.execute <<~_
SELECT * FROM users WHERE id = 1;
_
```
I've proposed a similar feature a while back and chimed in on similar proposals, but after thinking about it for a bit I realized "Anonymous HEREDOC blocks" is a slightly different approach that could be a different possible path forward.
--
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/lists/ruby-core.ml.ruby-lang.org/