From: "Eregon (Benoit Daloze) via ruby-core" Date: 2023-01-06T15:37:48+00:00 Subject: [ruby-core:111702] [Ruby master Bug#18797] Third argument to Regexp.new is a bit broken Issue #18797 has been updated by Eregon (Benoit Daloze). Assignee set to jeremyevans0 (Jeremy Evans) Target version set to 3.3 ---------------------------------------- Bug #18797: Third argument to Regexp.new is a bit broken https://bugs.ruby-lang.org/issues/18797#change-101098 * Author: janosch-x (Janosch M��ller) * Status: Open * Priority: Normal * Assignee: jeremyevans0 (Jeremy Evans) * Target version: 3.3 * 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/re.c#L3622-L3658). There is also a test for the resulting encoding [here](https://github.com/ruby/ruby/blob/cf2bbcfff2985c116552967c7c4522f4630f2d18/test/ruby/test_regexp.rb#L564), 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. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/