Author: caleb clausen
Status: Open, Priority: Normal
ruby -v: ruby 1.9.1p376 (2009-12-07 revision 26040) [x86_64-linux]
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 `<main>': 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.
----------------------------------------
http://redmine.ruby-lang.org
Status changed from Open to Assigned
Assigned to set to Yukihiro Matsumoto
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
----------------------------------------
http://redmine.ruby-lang.org/issues/show/2547
----------------------------------------
http://redmine.ruby-lang.org
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.