Ruby 2.6.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Regexpクラス > ~
~ self -> Integer | nil
[permalink][rdoc]変数 $_ の値との間でのマッチをとります。
ちょうど以下と同じ意味です。
self =~ $_
$_ = "hogehoge"
if /foo/
puts "match"
else
puts "no match"
end
# => no match
# ただし、警告がでる。warning: regex literal in condition
reg = Regexp.compile("foo")
if ~ reg
puts "match"
else
puts "no match"
end
# => no match
if reg
puts "match"
else
puts "no match"
end
# => match
# reg は nil でも false でも無いので常にtrue