From: duerst@...
Date: 2017-11-20T11:08:04+00:00
Subject: [ruby-core:83839] [Ruby trunk Feature#12700] regexg heredoc support

Issue #12700 has been updated by duerst (Martin D��rst).


shyouhei (Shyouhei Urabe) wrote:
> I had a chance to write a regexp constant consists of 300+ lines.
> I have to admit that I did wish I could write that using a heredoc.
> 
> So I changed my mind.  Let me +1.

If that 300+ lines regexp is public (or can be made public), I'd like to see a pointer.

There may be exceptions, but I don't think it's a good idea to write a regexp constant with 300+ lines by hand.
(The regular expression pieces in https://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/unicode_normalize/tables.rb?view=markup are way shorter than 300 lines, but I wouldn't have wanted to write them by hand anyway.)

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.

Just some points; I'm not too strongly against introducing this.


----------------------------------------
Feature #12700: regexg heredoc support
https://bugs.ruby-lang.org/issues/12700#change-67872

* Author: gam3 (Allen Morris)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
There is support for ', ", and ` heredocs, but there is no support for /.

Example code with new feature:

~~~ ruby
first = 'first'

orig = /#{first}
match\s # match
this    # match this
/x

right = <</REGEXP/x
#{first}
match\s # match
this    # match this
REGEXP

raise unless orig == right
~~~

There is no straight forward way to replace a regexp heredoc as a double quote heredoc requires that '\\s' be escaped.
As shown in the code below you can't use the string heredoc to directly replace a regexp heredoc because of this need for extra escaping.

~~~ ruby
first = 'first'

orig = /#{first}
match\s # match
this    # match this
/x

wrong = Regexp.new(<<REGEXP, Regexp::EXTENDED)
#{first}
match\s # match
this    # match this
REGEXP

right = Regexp.new(<<REGEXP, Regexp::EXTENDED)
#{first}
match\\s # match
this    # match this
REGEXP

raise unless orig != wrong
raise unless orig == right
~~~








---Files--------------------------------
regex_heredoc_patch (2.99 KB)


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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>