From: ko1@... Date: 2017-09-06T09:32:01+00:00 Subject: [ruby-core:82670] [Ruby trunk Feature#8948] Frozen regex Issue #8948 has been updated by ko1 (Koichi Sasada). Now we can't run `make test-all` because `test/lib/test/unit.rb` defines singleton method for a regexp object. ``` def non_options(files, options) filter = options[:filter] if filter pos_pat = /\A\/(.*)\/\z/ neg_pat = /\A!\/(.*)\/\z/ negative, positive = filter.partition {|s| neg_pat =~ s} if positive.empty? filter = nil elsif negative.empty? and positive.size == 1 and pos_pat !~ positive[0] filter = positive[0] else filter = Regexp.union(*positive.map! {|s| Regexp.new(s[pos_pat, 1] || "\\A#{Regexp.quote(s)}\\z")}) end unless negative.empty? negative = Regexp.union(*negative.map! {|s| Regexp.new(s[neg_pat, 1])}) filter = /\A(?=.*#{filter})(?!.*#{negative})/ end if Regexp === filter # bypass conversion in minitest def filter.=~(other) # :nodoc: <-- singleton method!! super unless Regexp === other end end options[:filter] = filter end true end ``` Is it acceptable (should we allow such code)? ---------------------------------------- Feature #8948: Frozen regex https://bugs.ruby-lang.org/issues/8948#change-66500 * Author: sawa (Tsuyoshi Sawada) * Status: Assigned * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: ---------------------------------------- =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/ Unsubscribe: