From: "mame (Yusuke Endoh) via ruby-core" Date: 2025-12-11T12:06:38+00:00 Subject: [ruby-core:124137] [Ruby Feature#8948] Frozen regex Issue #8948 has been updated by mame (Yusuke Endoh). Here is the code we found in the dev meeting: https://github.com/ctrochalakis/mongo-ruby-driver/blob/e632eb570d2f42bddc6703c88d3ce43aea6cdfe6/lib/mongo/types/regexp_of_holding.rb#L27-L41 ```ruby class LazyRegexp < Regexp ... def initialize(source, **options) @lazy_source = source super(source.to_s, **options) end ``` https://github.com/wallaby-rails/wallaby-rails/blob/e1d2e748887bbbdf024eb24657c91f56cc9985f9/wallaby-core/lib/routes/wallaby/lazy_regexp.rb#L10-L16 ```ruby class RegexpOfHolding < Regexp ... def initialize(str, options, extra_options_str) ... super(str, options) @extra_options_str = extra_options_str end end ``` Also, your PR changes the behavior of `Marshal.load` for a Regexp with encoding: ```diff - assert_equal(/u/, Marshal.load("\004\bI/\a\\u\000\006:\016@encoding\"\vEUC-JP")) + assert_raise(FrozenError) { Marshal.load("\004\bI/\a\\u\000\006:\016@encoding\"\vEUC-JP") } ``` This incompatibility should be fixed. Also, as a general rule, please clearly state when a PR includes such known incompatibilities. ---------------------------------------- Feature #8948: Frozen regex https://bugs.ruby-lang.org/issues/8948#change-115593 * Author: sawa (Tsuyoshi Sawada) * Status: Assigned * Assignee: Eregon (Benoit Daloze) ---------------------------------------- =begin I see that frozen string was accepted for Ruby 2.1, and frozen array and hash are proposed in https://bugs.ruby-lang.org/issues/8909. I feel there is even more use case for a frozen regex, i.e., a regex literal that generates a regex only once. It is frequent to have a regex within a frequently repeated portion of code, and generating the same regex each time is a waste of resource. At the moment, we can have a code like: class Foo RE1 = /pattern1/ RE2 = /pattern1/ RE3 = /pattern1/ def classify case self when RE1 then 1 when RE2 then 2 when RE3 then 3 else 4 end end end but suppose we have a frozen `Regexp` literal `//f`. Then we can write like: class Foo def classify case self when /pattern1/f then 1 when /pattern1/f then 2 when /pattern1/f then 3 else 4 end end end =end -- 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/lists/ruby-core.ml.ruby-lang.org/