Project

General

Profile

Actions

Bug #13371

closed

Syntax Error with regex when parens are omitted

Added by ndn (Nikola Nenkov) about 7 years ago. Updated about 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:80377]

Description

When you skip the parens of method invocations and pass a regex, you can get a SyntaxError if the regex starts with a space.

''.split(/ ./) # => []
''.split /./   # => []
''.split / ./  # !> SyntaxError: unexpected '.'

As per the syntax highlighting in my text editor and a discussion with another person, it seems that the opening slash is interpreted as division:

require 'ripper'

Ripper.lex("''.split /./")
  # =>
  # [[[1, 0], :on_tstring_beg, "'"],
  #  [[1, 1], :on_tstring_end, "'"],
  #  [[1, 2], :on_period, "."],
  #  [[1, 3], :on_ident, "split"],
  #  [[1, 8], :on_sp, " "],
  #  [[1, 9], :on_regexp_beg, "/"],
  #  #         ^^^
  #  [[1, 10], :on_tstring_content, "."],
  #  [[1, 11], :on_regexp_end, "/"]]

Ripper.lex("''.split / ./")
  # =>
  # [[[1, 0], :on_tstring_beg, "'"],
  #  [[1, 1], :on_tstring_end, "'"],
  #  [[1, 2], :on_period, "."],
  #  [[1, 3], :on_ident, "split"],
  #  [[1, 8], :on_sp, " "],
  #  [[1, 9], :on_op, "/"],
  #  #         ^^^
  #  [[1, 10], :on_sp, " "],
  #  [[1, 11], :on_period, "."],
  #  [[1, 12], :on_op, "/"]]

Reproducible with older versions as well (tried it until 2.1.2)

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0