https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112016-08-25T17:40:35ZRuby Issue Tracking SystemRuby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=602872016-08-25T17:40:35Zgam3 (Allen Morris)gam3-ruby@gam3.net
<ul></ul><p>Added pull request <a href="https://github.com/ruby/ruby/pull/1418" class="external">https://github.com/ruby/ruby/pull/1418</a></p> Ruby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=603162016-08-29T14:41:33Zgam3 (Allen Morris)gam3-ruby@gam3.net
<ul></ul><p>Updated pull request.</p> Ruby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=604162016-09-07T08:47:25Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>Use <code>%r</code>.</p>
<p>Matz.</p> Ruby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=605332016-09-16T13:57:39Zgam3 (Allen Morris)gam3-ruby@gam3.net
<ul></ul><p>I don't see how %r helps.</p>
<p>Here is an (rather forced) example of the advantage of a /HEREDOC/</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="s2">"one"</span>
<span class="n">b</span> <span class="o">=</span> <span class="s2">"two"</span>
<span class="k">raise</span> <span class="s2">"error"</span> <span class="k">unless</span> <span class="n">a</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="o"><<</span><span class="sr">/REG/x</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">b</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="o"><<</span><span class="sr">/REG/x</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
<span class="p">(</span><span class="o">.</span><span class="p">)</span> <span class="c1"># what we want to match</span>
<span class="n">n</span> <span class="c1"># what we want to skip</span>
<span class="n">e</span> <span class="c1"># more to skip</span>
<span class="no">REG</span>
<span class="n">t</span> <span class="c1"># what we want to skip</span>
<span class="n">w</span> <span class="c1"># more to skip</span>
<span class="p">(</span><span class="o">.</span><span class="p">)</span> <span class="c1"># what we want to match</span>
<span class="no">REG</span>
<span class="k">raise</span> <span class="s2">"error"</span> <span class="k">unless</span> <span class="n">a</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="sr">%r|
(.) # what we want to match
n # what we want to skip
e # more to skip
|x</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">b</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="sr">%r|
t # what we want to skip
w # more to skip
(.) # what we want to match
|x</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
</code></pre> Ruby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=606222016-09-24T02:45:38Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Open</i></li></ul><p>Reopened.</p>
<p>Though I have never needed to write such long regexp literals inline, privately.<br>
Whenever I wanted multine superb regexps they are named, most likely become constants.<br>
For variable/constant assignments, %r perfectly works. I doubt the actual needs of this syntax.</p> Ruby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=606292016-09-24T04:26:00Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>Shyouhei Urabe wrote:</p>
<blockquote>
<p>Reopened.</p>
<p>Though I have never needed to write such long regexp literals inline, privately.<br>
Whenever I wanted multine superb regexps they are named, most likely become constants.<br>
For variable/constant assignments, %r perfectly works. I doubt the actual needs of this syntax.</p>
</blockquote>
<p>I'm confused. You only give arguments for rejection, but then reopen the issue.</p> Ruby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=606302016-09-24T07:53:18Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>Martin Dürst wrote:</p>
<blockquote>
<p>I'm confused. You only give arguments for rejection, but then reopen the issue.</p>
</blockquote>
<p>My private opinion is I don't need this.<br>
But I don't want to rule out my being wrong-headed.<br>
The OP might have other use-case where this is useful.<br>
So the reopen.<br>
Everyone who need this feature are encouraged to involve this thread.</p> Ruby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=678702017-11-20T10:41:29Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>I had a chance to write a regexp constant consists of 300+ lines.<br>
I have to admit that I did wish I could write that using a heredoc.</p>
<p>So I changed my mind. Let me +1.</p> Ruby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=678722017-11-20T11:08:03Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>shyouhei (Shyouhei Urabe) wrote:</p>
<blockquote>
<p>I had a chance to write a regexp constant consists of 300+ lines.<br>
I have to admit that I did wish I could write that using a heredoc.</p>
<p>So I changed my mind. Let me +1.</p>
</blockquote>
<p>If that 300+ lines regexp is public (or can be made public), I'd like to see a pointer.</p>
<p>There may be exceptions, but I don't think it's a good idea to write a regexp constant with 300+ lines by hand.<br>
(The regular expression pieces in <a href="https://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/unicode_normalize/tables.rb?view=markup" class="external">https://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/unicode_normalize/tables.rb?view=markup</a> are way shorter than 300 lines, but I wouldn't have wanted to write them by hand anyway.)</p>
<p>Looking at the examples above, the advantages for the regexp heredoc over %r seem to be the fact that two or more of them can be started in the same line (including the options). The advantage over indirect construction via string heredoc seems to be that no double escape is necessary. None of these advantages seems directly related to the length of the regexp.</p>
<p>Just some points; I'm not too strongly against introducing this.</p> Ruby master - Feature #12700: regexg heredoc supporthttps://redmine.ruby-lang.org/issues/12700?journal_id=679092017-11-24T00:53:20Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>duerst (Martin Dürst) wrote:</p>
<blockquote>
<blockquote>
<p>So I changed my mind. Let me +1.</p>
</blockquote>
<p>If that 300+ lines regexp is public (or can be made public), I'd like to see a pointer.</p>
</blockquote>
<p>Here you are: <a href="https://github.com/shyouhei/optdown/blob/master/lib/optdown/expr.rb" class="external">https://github.com/shyouhei/optdown/blob/master/lib/optdown/expr.rb</a></p>
<blockquote>
<p>There may be exceptions, but I don't think it's a good idea to write a regexp constant with 300+ lines by hand.<br>
(The regular expression pieces in <a href="https://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/unicode_normalize/tables.rb?view=markup" class="external">https://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/unicode_normalize/tables.rb?view=markup</a> are way shorter than 300 lines, but I wouldn't have wanted to write them by hand anyway.)</p>
</blockquote>
<p>I have to agree with this part from the bottom of my heart. It was a wrong decision for me to write the expression above. I should have used something different.</p>
<p>But I did it anyway because it seemed possible. And it was. The experience was terrible as expected. A regexp heredoc should absorb some part of the pain I believe.</p>
<blockquote>
<p>Looking at the examples above, the advantages for the regexp heredoc over %r seem to be the fact that two or more of them can be started in the same line (including the options). The advantage over indirect construction via string heredoc seems to be that no double escape is necessary. None of these advantages seems directly related to the length of the regexp.</p>
</blockquote>
<p>True.</p>
<p>What I found during writing a long regular expression is that the expression seemed to contain all the possible punctuation character to be used in %r. Two or more characters to terminate the expression seemed the right solution. At the same time I needed to interpolate variables into the expression I could not use <code>Regexp.new(<<END)</code> -- that way I had to double all the backslashes. These two are the main reasons I changed my mind to +1 this request.</p>
<blockquote>
<p>Just some points; I'm not too strongly against introducing this.</p>
</blockquote>
<p>We can live without heredoc regexps. In fact I do in the example above. However, that is also true for all other sort of heredocs; we don't need them in theory if we could properly escape everything. In practice that is too annoying. The same goes with regexp literals I believe.</p>