From the operator precedence table (highest to lowest): [] []= ** ! ~ + - [一元] [多几行] <=> == === != =~ !~ [] []= ** ! ~ + - [unary] [several more lines] <=> == === != =~ !~此外,Regexp类具有一元~运算符:Also, the Regexp class has a unary ~ operator: 〜rxp→整数或nil 匹配-将rxp与$_的内容匹配.等同于rxp =~ $_. ~ rxp → integer or nil Match—Matches rxp against the contents of $_. Equivalent to rxp =~ $_.所以您的表情等同于:"abc" != (/abc/ =~ $_)和 Regexp#=~ 运算符(不是与更熟悉的 String#=~ )返回一个数字:And the Regexp#=~ operator (not the same as the more familiar String#=~) returns a number: rxp =〜str→整数或nil 匹配-将rxp与str匹配. rxp =~ str → integer or nil Match—Matches rxp against str.因为将字符串与数字进行比较是错误的,所以最终结果为true.So you get true as your final result because comparing a string to a number is false.例如:>> $_ = 'Where is pancakes house?'=> "Where is pancakes house?">> 9 !=~ /pancakes/=> false>> ~ /pancakes/=> 9 这篇关于红宝石中的!=〜比较运算符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 18:31