instance method Regexp#~

~ self -> Integer | nil[permalink][rdoc][edit]

変数 $_ の値との間でのマッチをとります。

ちょうど以下と同じ意味です。


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