Ruby 4.0 リファレンスマニュアル

singleton method Regexp.try_convert

try_convert(obj) -> Regexp | nilRuby 1.9.3 から[permalink][rdoc][edit]
(Regexp | _ToRegexp regexp_like) → Regexp
(untyped other) → Regexp?

obj を正規表現オブジェクトに変換するためのメソッドです。

obj が正規表現オブジェクトならそのまま obj を返します。

obj が正規表現オブジェクトでないとき、to_regexp メソッドに応答するなら obj.to_regexp の結果を返し、応答しないなら nil を返します。

p Regexp.try_convert(/re/)    # => /re/
p Regexp.try_convert("re")    # => nil