arscariosus
arscariosus

Reputation: 1356

Confused with Ruby's <=> operator

I am confused with Ruby's <=> operator. How does it differ from == or ===? Any comprehensive examples/use case? Thanks.

Upvotes: 7

Views: 7488

Answers (3)

c2h2
c2h2

Reputation: 12349

== will NOT work in sort for example

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

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

Upvotes: 2

ghostdog74
ghostdog74

Reputation: 342313

<=> 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

Upvotes: 14

Dogbert
Dogbert

Reputation: 222108

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

Upvotes: 9

Related Questions