From: duerst via ruby-core <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: duerst <ruby-core@ml.ruby-lang.org>
Subject: [ruby-core:113402] [Ruby master Feature#19015] Language extension by a heredoc
Date: Thu, 04 May 2023 04:03:10 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-102973.20230504040309.17@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19015.20220922032218.17@ruby-lang.org
Issue #19015 has been updated by duerst (Martin Dürst).
@pyromaniac I think the main problem would be how to handle namespacing. With single letters, the chance of collision is very high. How does Elixir handle this?
----------------------------------------
Feature #19015: Language extension by a heredoc
https://bugs.ruby-lang.org/issues/19015#change-102973
* Author: ko1 (Koichi Sasada)
* Status: Open
* Priority: Normal
----------------------------------------
This propose new heredoc extension with `<<!LANG` like
```ruby
doc = <<!LANG
# description written in lang LANG
foo bar
LANG
```
and it is translated to:
```ruby
doc = heredoc_extension_LANG(heredoc_text, binding)
```
## Example
```ruby
require 'erb'
def heredoc_extension_erb str, b
ERB.new(str).run(b)
end
name = 'ko1'
html = <<!erb
<div>Hello <%= name %></div>
erb
puts html #=> <div>Hello ko1</div>
```
## Background / considerations
* Sometimes we write Ruby syntax string with `<<RUBY` and this proposal inspired by it.
* it is similar to shebang (`#!LANG` in shell)
* [Elixir's custom sigil](https://elixir-lang.org/getting-started/sigils.html) translates `~u(...)` translates to `sigil_u(...)`. This is why it translated to `heredoc_extension_LANG(...)` private method call.
* JavaScript has JSX but I don't think it is fit to the Ruby language.
* Heredoc is Ruby's chaos part and already confusing a lot. Additional chaos doesn't matter.
* `<<!foo` is valid syntax but now I don't think it is not used. gem codesearch doesn't find the usage.
* Sorry I couldn't wait 1st/Apr.
## Implementation
I attached the experimental implementation which only supports `erb` (because I couldn't find how to get delimiter to determine a method name :p).
---Files--------------------------------
heredoc_extension.patch (2.7 KB)
--
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/
next prev parent reply other threads:[~2023-05-04 4:03 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-22 3:22 [ruby-core:109985] [Ruby master Feature#19015] Language extension by a heredoc ko1 (Koichi Sasada)
2022-09-22 7:38 ` [ruby-core:109992] " ko1 (Koichi Sasada)
2022-09-22 8:30 ` [ruby-core:109993] " retro
2022-09-22 8:46 ` [ruby-core:109994] " zverok (Victor Shepelev)
2022-09-22 10:51 ` [ruby-core:109997] " yugui (Yuki Sonoda)
2022-09-22 17:56 ` [ruby-core:109998] " ko1 (Koichi Sasada)
2022-09-22 23:18 ` [ruby-core:110015] " estum (Anton (estum))
2022-09-26 4:45 ` [ruby-core:110080] " duerst
2022-09-27 18:30 ` [ruby-core:110112] " Eregon (Benoit Daloze)
2022-10-24 8:14 ` [ruby-core:110495] " ko1 (Koichi Sasada)
2022-11-03 21:38 ` [ruby-core:110597] " dsisnero (Dominic Sisneros)
2023-05-04 3:24 ` [ruby-core:113401] " pyromaniac (Arkadiy Zabazhanov) via ruby-core
2023-05-04 4:03 ` duerst via ruby-core [this message]
2023-05-04 4:43 ` [ruby-core:113403] " pyromaniac (Arkadiy Zabazhanov) via ruby-core
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-102973.20230504040309.17@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
--cc=ruby-core@ml.ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).