Method: Regexp#initialize

Defined in:
re.c

#new(string, options = 0, timeout: nil) ⇒ Regexp #new(regexp, timeout: nil) ⇒ Regexp

With argument string given, returns a new regexp with the given string and options:

r = Regexp.new('foo') # => /foo/
r.source              # => "foo"
r.options             # => 0

Optional argument options is one of the following:

If optional keyword argument timeout is given, its float value overrides the timeout interval for the class, Regexp.timeout. If nil is passed as +timeout, it uses the timeout interval for the class, Regexp.timeout.

With argument regexp given, returns a new regexp. The source, options, timeout are the same as regexp. options and n_flag arguments are ineffective. The timeout can be overridden by timeout keyword.

options = Regexp::MULTILINE
r = Regexp.new('foo', options, timeout: 1.1) # => /foo/m
r2 = Regexp.new(r)                           # => /foo/m
r2.timeout                                   # => 1.1
r3 = Regexp.new(r, timeout: 3.14)            # => /foo/m
r3.timeout                                   # => 3.14

Overloads:

  • #new(string, options = 0, timeout: nil) ⇒ Regexp
  • #new(regexp, timeout: nil) ⇒ Regexp

4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
# File 're.c', line 4004

static VALUE
rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
{
    struct reg_init_args args;
    VALUE re = reg_extract_args(argc, argv, &args);

    if (NIL_P(re)) {
        reg_init_args(self, args.str, args.enc, args.flags);
    }
    else {
        reg_copy(self, re);
    }

    set_timeout(&RREGEXP_PTR(self)->timelimit, args.timeout);

    return self;
}