Project

General

Profile

Actions

Feature #20266

open

New syntax to escape embed strings in Regexp literal

Added by usa (Usaku NAKAMURA) 3 months ago. Updated 3 months ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:116769]

Description

Premise

When using embed strings in Regexp literal, it is interpreted as a part of the Regexp.

foo = "[a-z]"
p /#{foo}/ #=> /[a-z]/

So, currently we often have to escape the embed strings.

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:

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.

Actions

Also available in: Atom PDF

Like1
Like0Like0Like0Like0Like0Like1Like0