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

singleton method Regexp.try_convert

try_convert(obj) -> Regexp | nilRuby 1.9.3 から[permalink][rdoc][edit]

obj を to_regexp メソッドで Regexp オブジェクトに変換しようと試みます。

変換に成功した場合はそれを返し、失敗時には nil を返します。

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