与Ruby的< =>混淆操作者

时间:2011-01-20 10:46:07

标签: ruby spaceship-operator

我对Ruby的< =>感到困惑运营商。它与==或===有什么不同?任何综合的例子/用例?感谢。

3 个答案:

答案 0 :(得分:13)

<=>是组合比较运算符。如果LHS等于RHS,则返回0;如果LHS大于RHS,则返回1;如果LHS小于RH,则返回-1

答案 1 :(得分:9)

它被称为“宇宙飞船”运营商。更多信息:What is the Ruby <=> (spaceship) operator?http://en.wikipedia.org/wiki/Spaceship_operator

答案 2 :(得分:2)

==不会在排序中工作,例如

[3,5,6,2,7].sort{|x,y| x <=>y }

==返回布尔值
&LT; =&GT;返回Fixnum(-1,0,1)

相关问题