What is the <=> operator in ruby?

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In ruby, the <=> operator is known as the combined comparison operator.  It is also known loosely as the “spaceship operator” because it looks like a spaceship.

It compares the first operand with the second operand.  If the former is greater than the latter, the operator returns the value 1.  If the first operand is less than then second operand, the <=> operator returns -1.  If the two operands are equal, the operator returns 0.

So the expression

a <=> b

will return -1 if “a” is 5 and “b” is 10.