Issue #20266 has been updated by rubyFeedback (robert heiler).
I don't have any pro or con opinion on the feature itself; in regards to ^foo versus
=foo, I think users may wonder about both: ^ specifically because many regexes may
have it, such as /^foobar/, and with = they may assume some assignment to be made.
At the least that was my first impression when seeing it, perhaps inspired by erb.
I guess **IF** the rationale is that Regexp.quote(i) is too long to type, which seems a
reasonable statement, then it makes sense to use a shorthand syntax. But probably all
shorthand syntaxes here may not be "perfect". Remember the perl-inspired $
variables;
not everyone can remember them easily. (Unfortunately the longer $ named variables
weren't a big improvement either.)
Just for sake of completion, as this was already discussed, could someone show
alternative syntax suggestions, if they were made? Just so we can more easily compare
the preferred variant over the other variants, e. g. two so far, even if one may be
"inofficial" by shan:
#{^foo}
#{=foo}
I'll also try shan's suggestion via the first one, as "side-by-side"
comparison:
foo = "[a-z]"
p /#{=foo}/
foo = "[a-z]"
p /#{^foo}/
Hmm. And my initial thought of the second one used with leading ^
p /^#{^foo}/
And for comparison the other one also with leading ^:
p /^#{=foo}/
I think none of them will win any beauty contest, but it could still be
interesting for a comparison.
----------------------------------------
Feature #20266: New syntax to escape embed strings in Regexp literal
https://bugs.ruby-lang.org/issues/20266#change-106813
* Author: usa (Usaku NAKAMURA)
* Status: Open
* Priority: Normal
----------------------------------------
# Premise
When using embed strings in Regexp literal, it is interpreted as a part of the Regexp.
```ruby
foo = "[a-z]"
p /#{foo}/ #=> /[a-z]/
```
So, currently we often have to escape the embed strings.
```ruby
foo = "[a-z]"
p /#{Regexp.quote(foo)}/ #=> /\[a\-z\]/
```
This is very long and painful to write every time.
So, I propose new syntax to escape embed strings automatically.
# Proposal
Adding new token `#{=` in Regexp literal:
```ruby
foo = "[a-z]"
p /#{=foo}/ #=> /\[a\-z\]/
```
When `#{=` is used instead of `#{`, ruby calls `Regexp.quote` internally.
# Compatibility
Current ruby causes syntax error when using `#{=`, then there is no incompatibilty.
# Out of scope of this proposal
I do not propose about `#{=` in another literals. They are out of scope of this
proposal.
--
https://bugs.ruby-lang.org/