Ruby 2.1.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Regexpクラス > ~
~ self -> Fixnum | 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