https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112014-03-26T13:40:54ZRuby Issue Tracking SystemRuby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459462014-03-26T13:40:54Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>And the expected output?</p> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459472014-03-26T13:47:17Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Sorry, of course:</p>
<pre><code>f(42)
</code></pre>
<p>i would expect to output</p>
<pre><code>Literal text
------------
Here text.
Text with interpolation and escapes
-----------------------------------
Here text with interpolation: 42.
</code></pre>
<p>(There should be one more blank line at the end.)</p> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459482014-03-26T13:52:32Zalexeymuranov (Alexey Muranov)
<ul></ul><p>IMO, this would allow a program with heredocs to be easily readable without having to indent heredocs. Especially if editor's syntax highlighting will highlight <code>></code> and <code>>></code>.</p> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459542014-03-27T01:42:56Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>Could you summarize your proposal concretely?</p> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459592014-03-27T13:36:43Zalexeymuranov (Alexey Muranov)
<ul></ul><p>I try to summarize again, hopefully better, and with a bit different syntax.</p>
<a name="Example"></a>
<h3 >Example<a href="#Example" class="wiki-anchor">¶</a></h3>
<pre><code>class C
def f(x)
print <<Message1: + <<Message2: + <<Message3:
Message1:
> 1. Some text
> without any intepolation (#{ x }) or escape sequences (\n)
>
Message2:
>> 2. Some text\
>> with interpolation (#{ x }) and escape sequences.\n
Message3:
>> 3. Some mixed text: \
> #{ x } is replaced with
>> #{ x }
end
end
C.new.f(42)
</code></pre>
<p>should print</p>
<pre><code>1. Some text
without any intepolation (#{ x }) or escape sequences (\n)
2. Some text with interpolation (42) and escape sequences.
3. Some mixed text: #{ x } is replaced with
42
</code></pre>
<a name="Explanation"></a>
<h3 >Explanation<a href="#Explanation" class="wiki-anchor">¶</a></h3>
<p>In each line preceeded with single <code>></code>, the leading <code>></code> and one space are<br>
removed, and the rest is interpreted as a single-quoted string:</p>
<pre><code>> <line content>
</code></pre>
<p>is the same as</p>
<pre><code>'<line content>
'
</code></pre>
<p>In each line preceeded with <code>>></code>, the leading <code>>></code> and one space are<br>
removed, and the rest is interpreted as a double-quoted string:</p>
<pre><code>>> <line content>
</code></pre>
<p>is the same as</p>
<pre><code>"<line content>
"
</code></pre>
<p>Then lines are concatenated.</p>
<p>I hope my proposal is more clear now.</p>
<p><em>Update.</em> The only difference in interpretation with quoted strings will probably be that quotes would not need to be escaped.</p> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459672014-03-27T19:02:07Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Is it possible to start the '>' anywhere besides the first char?</p>
<pre><code>def my_method
a = <<Message1:
Message1:
> Some
> content
end
</code></pre>
<p>Or even something anonymous like:</p>
<pre><code>def my_method
a = <<:
> Some
> content
end
</code></pre> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459692014-03-27T19:08:13Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Rodrigo Rosenfeld Rosas wrote:</p>
<blockquote>
<p>Is it possible to start the '>' anywhere besides the first char?</p>
</blockquote>
<p>Good point, why not. My motivation was however to be able to write heredocs without indentation in a nice and clear way.</p>
<p>I have just thought of proposing exactly the same "anonymous" syntax.</p> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459702014-03-27T19:12:59Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>What should the scope for interpolation be?</p>
<pre><code>def my_method
a = 1
b = <<:message
end
a = 2
message:
>> #{a}
my_method # what is the output?
</code></pre> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459712014-03-27T20:13:55Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Rodrigo Rosenfeld Rosas wrote:</p>
<blockquote>
<p>What should the scope for interpolation be?</p>
</blockquote>
<p>I had thought about it, but i did not see a better option than to require the heredoc to be defined immediately after the line where it is used, as usual.</p> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459722014-03-27T20:16:27Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Yes, it has to be defined immediately, otherwise it would be impossible to reuse the same heredoc "identifier" (<code>Message:</code>), and it may conflict with program identifiers.</p> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=459802014-03-28T10:56:14Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Some use cases for fun:</p>
<pre><code>system <<:
> ./configure
> make
> make install
eval <<:
> 10.times do
> puts <<:
> > Who is the silly person who wrote this program?
> end
</code></pre> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=460822014-04-04T20:16:29Zalexeymuranov (Alexey Muranov)
<ul></ul><p>I have just realized this would cause a difficulty with pasting code into <code>irb</code>. This would not be the first, however, -- the following cannot be pasted into <code>irb</code> either:</p>
<pre><code>class C
def f
1
end
end
puts C.new
.f
</code></pre> Ruby master - Feature #9678: New heredoc syntaxhttps://redmine.ruby-lang.org/issues/9678?journal_id=955332021-12-23T23:40:34Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Project</strong> changed from <i>14</i> to <i>Ruby master</i></li></ul>