From: eregontp@... Date: 2021-04-02T10:28:02+00:00 Subject: [ruby-core:103176] [Ruby master Feature#17771] String#start_with? should not construct MatchData or set $~ Issue #17771 has been updated by Eregon (Benoit Daloze). I don't think there is a rule that predicate methods only return a boolean and never set `$~`. It is the case for `String#match` vs `String#match?`, but it doesn't mean it holds for other Regexp methods. I see it a bit like the use of `!`, which in the core library is generally only used if there is also a non-`!` variant (e.g., `Array#delete`). `String#start_with?` enables to match a regexp without the need to manually build another regexp like `/\A#{regexp}/` (from the user point of view, there might be internal caching depending on the regexp engine), so I think that is a valid use case for using `start_with?` and accessing the MatchData after. StringScanner has a similar functionality for matching a regexp from the start, as if there was a `\A`, but does not expose `$~` directly: `ruby -rstrscan -e 's = StringScanner.new("test string"); s.scan(/(\w)\w+/); p s[1]'` => `"t"`. That said, I'm not against no longer setting $~ for String#start_with?, but I do worry about the compatibility issue here, especially since it might be quite hard to debug why $~ is suddenly `nil` or the previous MatchData in the Ruby version changing this behavior. ---------------------------------------- Feature #17771: String#start_with? should not construct MatchData or set $~ https://bugs.ruby-lang.org/issues/17771#change-91251 * Author: headius (Charles Nutter) * Status: Open * Priority: Normal ---------------------------------------- I am working on making $~ more thread-safe in JRuby and came across this unexpected behavior: ```ruby $ rvm ruby-3.0 do ruby -e '"foo".start_with?(/foo/); p $~' # ``` The `start_with?` method was added 11 years ago in https://bugs.ruby-lang.org/issues/3388 but I do not think the set of $~ was an intended feature. The `start_with?` method could be much faster and more thread-safe if it did not use the frame-local backref slot and did not allocate a MatchData. Compare with `match?` which was added specifically (without MatchData or backref setting) to provide a fast way to check if a Regexp matches. I propose that `start_with?` stop constructing MatchData, stop setting backref, and provide only its boolean result in the same way as `match?`. -- https://bugs.ruby-lang.org/ Unsubscribe: