Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backslashes in %w() misbehave #12277

Open
HertzDevil opened this issue Jul 16, 2022 · 0 comments
Open

Backslashes in %w() misbehave #12277

HertzDevil opened this issue Jul 16, 2022 · 0 comments

Comments

@HertzDevil
Copy link
Contributor

Currently, backslashes inside a %w() string array literal may escape whitespaces and the literal delimiters: (contrast with #5403 where %q() cannot escape its own delimiters)

%w(\ \
\)\() # => [" \n)("]

However, backslashes here also indefinitely extend an escape sequence, and one \ is written for every extra backslash:

%w(\ a)   # => [" a"]
%w(\\ a)  # => ["\\ a"]
%w(\\\ a) # => ["\\\\ a"]
%w(\a)    # => ["\\a"]
%w(\\a)   # => ["\\\\a"]
%w(\\\a)  # => ["\\\\\\a"]

Consequently, it is not possible to use a backslash at the very end of an array, as any number of \s followed by the end delimiter merely escapes that delimiter:

%w(\) # Error: Unterminated string array literal
%w(\\) # Error: Unterminated string array literal
%w(\\)
#) # => ["\\)", "#"]

Here are the same literals in Ruby:

%w(\ a)   # => [" a"]
%w(\\ a)  # => ["\\", "a"]
%w(\\\ a) # => ["\\ a"]
%w(\a)    # => ["\\a"]
%w(\\a)   # => ["\\a"]
%w(\\\a)  # => ["\\\\a"]

%w(\)  # Error: unterminated list meets end of file
%w(\\) # => ["\\"]

%w(\\) # => ["\\"]
#)

I think we should follow suit and make \ escape only the following character. Like #5403, however, we should decide whether this would constitute a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant