Project

General

Profile

Actions

Bug #2547

closed

error when interpolating a string containing a single backslash into a regexp

Added by coatl (caleb clausen) over 14 years ago. Updated almost 13 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 1.9.1p376 (2009-12-07 revision 26040) [x86_64-linux]
Backport:
[ruby-core:27374]

Description

=begin
An error is raised if an interpolation in a regexp contains a string containing a single backslash. This case used to work just great in ruby 1.8. For example:

$ ruby19 -e 'a="\"; p /#{a}y/'
-e:1:in `': too short escape sequence (ArgumentError)

Note that if the string literal is inlined directly into the interpolation, the error does not happen. I'd guess that ruby optimizes this case away at parse time, and thus sidesteps the error:

$ ruby19 -e 'p /#{"\"}y/'
/\y/

I've tested this in 1.9.1; presumably 1.9.2 has the same behavior, but I don't have a 1.9.2 readily available.
=end

Actions #1

Updated by naruse (Yui NARUSE) over 14 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

=begin
trunk also reproduce this.

However this may be a feature:

  • Ruby's regexp forbids to split character escape like 'a="\"; p /#{a}y/'
  • /#{"\"}y/ is optimized as /\y/ on parser, so this is valid
    =end
Actions #2

Updated by coatl (caleb clausen) over 14 years ago

=begin
I can see why it would be easier to forbid splitting up escape sequences in regexp. But:

  1. Ruby 1.8 did handle this; it can't be all that impossible.

  2. If splitting escapes is forbidden, then for consistency, the directly inlined version should be forbidden as well.
    =end

Actions #3

Updated by nobu (Nobuyoshi Nakada) about 14 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

=begin
This issue was solved with changeset r27431.
caleb, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0