[ruby-core:77294] [Ruby trunk Feature#12700] regexg heredoc support
From:
gam3-ruby@...3.net
Date:
2016-09-16 13:57:39 UTC
List:
ruby-core #77294
Issue #12700 has been updated by Allen Morris.
I don't see how %r helps.
Here is an (rather forced) example of the advantage of a /HEREDOC/
~~~ ruby
a = "one"
b = "two"
raise "error" unless a.match(<</REG/x)[1] == b.match(<</REG/x)[1]
(.) # what we want to match
n # what we want to skip
e # more to skip
REG
t # what we want to skip
w # more to skip
(.) # what we want to match
REG
raise "error" unless a.match(%r|
(.) # what we want to match
n # what we want to skip
e # more to skip
|x)[1] == b.match(%r|
t # what we want to skip
w # more to skip
(.) # what we want to match
|x)[1]
~~~
----------------------------------------
Feature #12700: regexg heredoc support
https://bugs.ruby-lang.org/issues/12700#change-60533
* Author: Allen Morris
* Status: Rejected
* Priority: Normal
* Assignee:
----------------------------------------
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>