ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:109985] [Ruby master Feature#19015] Language extension by a heredoc
@ 2022-09-22  3:22 ko1 (Koichi Sasada)
  2022-09-22  7:38 ` [ruby-core:109992] " ko1 (Koichi Sasada)
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: ko1 (Koichi Sasada) @ 2022-09-22  3:22 UTC (permalink / raw)
  To: ruby-core

Issue #19015 has been reported by ko1 (Koichi Sasada).

----------------------------------------
Feature #19015: Language extension by a heredoc
https://bugs.ruby-lang.org/issues/19015

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

## 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--------------------------------
tmp.YubJ1EHi0O-xyzzy (2.7 KB)


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

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

end of thread, other threads:[~2023-05-04  4:43 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [ruby-core:113402] " duerst via ruby-core
2023-05-04  4:43 ` [ruby-core:113403] " pyromaniac (Arkadiy Zabazhanov) via ruby-core

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).