class method
Regexp.try_convert
(::Regexp | ::Regexp::_ToRegexp regexp_like) -> ::Regexp
(untyped other) -> ::Regexp?
Regexp.try_convert(object) → regexp or nil
re.c
View on GitHub ↗
static VALUE
rb_reg_s_try_convert(VALUE dummy, VALUE re)
{
return rb_check_regexp_type(re);
}
Returns object if it is a regexp:
Example
Regexp.try_convert(/re/) # => /re/
Otherwise if object responds to :to_regexp, calls object.to_regexp and returns the result.
Returns nil if object does not respond to :to_regexp.
Example
Regexp.try_convert('re') # => nil
Raises an exception unless object.to_regexp returns a regexp.