[ruby-core:111481] [Ruby master Bug#18797] Third argument to Regexp.new is a bit broken

Issue #18797 has been updated by jeremyevans0 (Jeremy Evans). Status changed from Closed to Open Pull request submitted to remove support for the 3rd argument: https://github.com/ruby/ruby/pull/7039 ---------------------------------------- Bug #18797: Third argument to Regexp.new is a bit broken https://bugs.ruby-lang.org/issues/18797#change-100850 * Author: janosch-x (Janosch MΓΌller) * Status: Open * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- ## Situation 'n' or 'N' can be passed as a third argument to `Regexp.new`. However, the behavior is not the same as the literal `n`-flag or the `Regexp::NOENCODING` option, and it makes the `#encoding` of `Regexp` and `Regexp#source` diverge: ```ruby /π /n # => SyntaxError Regexp.new('π ', Regexp::NOENCODING) # => RegexpError re = Regexp.new('π ', nil, 'n') # => /π / re.options == Regexp::NOENCODING # => true re.encoding # => ASCII-8BIT re.source.encoding # => UTF-8 re =~ 'π ' # => Encoding::CompatibilityError ``` ## Code [Here](https://github.com/ruby/ruby/blob/b41de3a1e8c36a5cc336b6f7cd3cb71126cf1a60/r...). There is also a test for the resulting encoding [here](https://github.com/ruby/ruby/blob/cf2bbcfff2985c116552967c7c4522f4630f2d18/t...), but it is a no-op because the whole file is set to that encoding via magic comment anyway. The third argument was added when ASCII was still the default Ruby encoding, so I guess Regexp and source encoding still matched at that point. ## Solution It could be fixed, but my impression is that it is not useful anymore. It was probably only added because `Regexp::NOENCODING` wasn't available at the time, so I think it could be deprecated like so:
Passing a third argument to Regexp.new is deprecated. Use `Regexp::NOENCODING` as second argument instead.
participants (1)
-
jeremyevans0 (Jeremy Evans)