Skip to content
instance method

Regexp#to_s

() -> ::String
to_s → string

Returns a string showing the options and string of self:

Example
r0 = /ab+c/ix
s0 = r0.to_s # => "(?ix-m:ab+c)"

The returned string may be used as an argument to Regexp.new, or as interpolated text for a Regexp interpolation:

Example
r1 = Regexp.new(s0) # => /(?ix-m:ab+c)/
r2 = /#{s0}/        # => /(?ix-m:ab+c)/

Note that r1 and r2 are not equal to r0 because their original strings are different:

Example
r0 == r1  # => false
r0.source # => "ab+c"
r1.source # => "(?ix-m:ab+c)"

Related: Regexp#inspect.

See also #to_json #timeout also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0