Bug #8628

Parse error with method argument that is a Hash with a specific pattern

Added by Dimitri Geshef over 2 years ago. Updated over 2 years ago.

ruby -v:ruby 2.0.0p195 (2013-05-14) [i386-mingw32] Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN


The next two lines cause a parse error

p /=/ => 'works'

/in comment/ => 'fails'

The reported error is:

syntax error, unexpected ',', expecting $end

/in comment/ => 'fails'


The issue is caused by the /=/ regexp in the method p call.

Notice that no parse error occurs when the Hash argument is put between parentheses

p(/=/ => 'works').

snippet.rb Magnifier (319 Bytes) Dimitri Geshef, 07/12/2013 03:33 PM


#1 Updated by Dimitri Geshef over 2 years ago

First detected in ruby 1.9.3p374 (2013-01-15) [i386-mingw32]

#2 Updated by Dimitri Geshef over 2 years ago

The error message when you run the snippet in attachment is:
snippet.rb:9: syntax error, unexpected tIDENTIFIER, expecting $end

/in comment/ => 'fails'


#3 Updated by Dimitri Geshef over 2 years ago

Could also reproduce the problem with the regexp /,/

In other words, this will also result in a parse error:

p /,/ => 'problematic too'

/in comment/ => 'parse fails in this line'

#4 Updated by Nobuyoshi Nakada over 2 years ago

  • Status changed from Open to Rejected

p /=/' is interpreted asp /= /.../', an assignment to a variable `p'.

#5 Updated by Dimitri Geshef over 2 years ago

My apologies for this false alarm.
Thank you for pointing the grammatical ambiguity:
/=/ can be parsed as the /= operator followed by a regexp literal or the regexp literal /=/ itself.
Didn't realize this.

Also available in: Atom PDF