From: tom.enebo@...
Date: 2021-04-01T19:04:25+00:00
Subject: [ruby-core:103156] [Ruby master Feature#17771] String#start_with? should not construct MatchData or set $~

Issue #17771 has been updated by enebo (Thomas Enebo).


It really feels like an unintended side-effect of the method.  If you write this method and accept a variable then depending on the type of that variable there is either some MatchData (MD) as a side-effect or there isn't.  This is inconsistent.  If you wanted to explicitly use MD then you have to know what you are supplying.  If you know it is a regexp then just writing str =~ /^my_pat/ is what you want.

----------------------------------------
Feature #17771: String#start_with? should not construct MatchData or set $~
https://bugs.ruby-lang.org/issues/17771#change-91229

* 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 $~'
#<MatchData "foo">
```

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: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>