Project

General

Profile

Actions

Feature #8948

open

Frozen regex

Added by sawa (Tsuyoshi Sawada) about 11 years ago. Updated over 1 year ago.

Status:
Assigned
Target version:
-
[ruby-core:57353]

Description

=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


Related issues 3 (0 open3 closed)

Related to Ruby master - Misc #17412: Regexp vs Range: freezing differencesClosedActions
Related to Ruby master - Feature #17256: Freeze all Regexp objectsClosedmatz (Yukihiro Matsumoto)Actions
Has duplicate Ruby master - Feature #16377: Regexp literals should be frozenClosedmame (Yusuke Endoh)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0