开发者

Confused with Ruby's <=> operator

开发者 https://www.devze.com 2023-02-06 05:40 出处:网络
I am confused with Ruby开发者_如何学运维\'s <=> operator. How does it differ from == or ===? Any comprehensive examples/use case? Thanks.<=> is the combined comparison operator. it returns 0

I am confused with Ruby开发者_如何学运维's <=> operator. How does it differ from == or ===? Any comprehensive examples/use case? Thanks.


<=> is the combined comparison operator. it returns 0 if LHS equals RHS, 1 if LHS is greater than the RHS and -1 if LHS is less than RHs


It's called the 'spaceship' operator. More info: What is the Ruby <=> (spaceship) operator? and http://en.wikipedia.org/wiki/Spaceship_operator


== will NOT work in sort for example

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

== returns Boolean
<=> returns Fixnum (-1,0,1)

0

精彩评论

暂无评论...
验证码 换一张
取 消