Possible Duplicate:
What is the Ruby <=> (spaceship) operator?
I saw a code and an operator I'm unfamiliar with
@array << {:suffix=> substring, :index => i}
@array.sort! { |x,y| x[:suffix] <=> y[:suffix]}
I can't do google on it. What does <=>
do?
This is the spaceship operator, it was borrowed from Perl. It is commonly used for sorting, because it returns -1 if left operand is less than right operand, 1 if right operand is greater than the left and returns 0 otherwise.
1 <=> 2 # => -1
2 <=> 1 # => 1
1 <=> 1 # => 0
It does comparison defined for the particular class. If it is the case that ... < ...
is true, it returns -1
, if ... == ...
is true, then 0
, and if ... > ...
is true, then 1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With