Project

General

Profile

Actions

Feature #19344

open

Regexp.new: stricter handling of second argument

Added by zverok (Victor Shepelev) almost 2 years ago. Updated almost 2 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:111818]

Description

Since Ruby 3.2 (#18788), the second argument to Regexp.new can be:

  1. Integer: then it is treated as a combination of Regexp::<constant> flags
  2. String: then it is treated as a combination of string flags
  3. nil or false: then it is ignored
  4. any other truthy value: then it is treated as an "ignore case" option.

The fourth one is confusing, especially since the introduction of the flexibility of flags: one might erroneously assume or forget the protocol, and the naive check will strengthen the assumption:

# maybe it accepts the array of string options?..
Regexp.new('foo', %w[i]) #=> /foo/i -- oh, seems it does

# would the symbol work?..
Regexp.new('foo', :i) #=> /foo/i -- awesome, it works!

I propose to change (4) to only handle literal true value, and raise TypeError on any unsupported type.

On compatibility: I believe that whenever the usage of boolean to distinguish "ignore case/respect case" is deliberate (like in rubygems), the code already passes true/false, not any random value. Otherwise, the change in Ruby 3.2 might also have broken it (what if it previously passed strings, meaning them to be "truthy values"?)

PS: BTW, the documentation for (4) was completely lost in Ruby 3.2, due to this PR. cc @jeremyevans0 (Jeremy Evans), @burdettelamar (Burdette Lamar)

Actions

Also available in: Atom PDF

Like0
Like0