class method · added in Ruby 3.2
Regexp.timeout=
[T < ::_ToF] (T timeout) -> T
Regexp.timeout = float or nil
re.c
View on GitHub ↗
static VALUE
rb_reg_s_timeout_set(VALUE dummy, VALUE timeout)
{
rb_ractor_ensure_main_ractor("can not access Regexp.timeout from non-main Ractors");
set_timeout(&rb_reg_match_time_limit, timeout);
return timeout;
}
It sets the default timeout interval for Regexp matching in second. nil means no default timeout configuration. This configuration is process-global. If you want to set timeout for each Regexp, use timeout keyword for Regexp.new.
Example
Regexp.timeout = 1 /^a*b?a*$/ =~ "a" * 100000 + "x" #=> regexp match timeout (RuntimeError)
Ruby version changes
-
3.2
-
Regexp.timeout=has been added. Also,Regexp.newnew supports timeout keyword. See [Feature #17837]
-