1

If we have <, >, and ==, the total order is determined by those. Why do we need <=>?

sawa
  • 165,429
  • 45
  • 277
  • 381
Adam Lee
  • 24,710
  • 51
  • 156
  • 236

5 Answers5

1

we Don't need <=>.

a<=>b

is equivalent to:

if a<b
  return -1
elsif a>b 
  return 1
else
  return 0
end

It is there for convenience and it was taken from perl.

Luke
  • 5,567
  • 4
  • 37
  • 66
  • I don't understand your point. We certainly need the method `<=>` as other built-in methods rely on it. And yes, the result of `<=>` could be obtained in other ways, but the same could be said about nearly all built-in methods. – Cary Swoveland Jun 11 '15 at 00:23
  • @CarySwoveland I see your point. The way I understood the question, OP was confused about what `<=>` did. To me it seemed like he was under the impression that <=> let you do something you could't do with `<`,`>`, and `=`. That was the premise under which I chose my wording. – Luke Jun 11 '15 at 00:33
  • @CarySwoveland I mean't "don't need" the same way we "don't need" `or` because we have `not` + `and` – Luke Jun 11 '15 at 00:35
  • @CarySwoveland I think LukeP means "we don't need [to explicitly define] `<=>` [given that `<`, `==`, and `>` are defined]." – sawa Jun 11 '15 at 00:46
0

<=> is the basis of Comparable, so you don't have to implement all of the compare functions yourself. It's easier and less error-prone to just implement one function instead of three.

Reactormonk
  • 21,472
  • 14
  • 74
  • 123
-1

The "spaceship" operator is for comparison, not equality. It's similar in concept to C's strcmp function.

From the String class:

string <=> other_string → -1, 0, +1 or nil

Comparison — Returns -1, 0, +1 or nil depending on whether string is less than, equal to, or greater than other_string.

In short, == returns a boolean expressing equality, while <=> returns a number expressing comparative value. If the first object is of greater value than the second, <=> returns +1. If it's of lesser value, -1 is returned. If the two have the same value, 0 is returned.

The "value" of an object can be defined to be just about anything. For String, however, <=> checks the lexicographic ordering of the two arguments.

Therefore:

"abc" == "abc" # true
("abc" <=> "abc") == 0 # true
Community
  • 1
  • 1
yossarian
  • 1,537
  • 14
  • 21
-1

This operator is sometimes called a "signum" function. It provides the most concise way to customize sort order. For example:

require "ostruct"

# Fake "rows" with OpenStructs
my_data = [
    OpenStruct.new({ :name => "Ben", :age => 50 }),
    OpenStruct.new({ :name => "Abe", :age => 50 }),
    OpenStruct.new({ :name => "Cab", :age => 51 })
]

# Sort by age descending, then name ascending
puts my_data.sort { |a, b| 2 * (b.age <=> a.age) + (a.name <=> b.name) }

This works because the value from <=> is always -1, 0, or 1. I don't know of a more efficient way to do general-purpose sorting.

lilole
  • 322
  • 2
  • 9
-1

You are correct that there is redundancy between <, ==, > and <=>. In fact, when <, ==, > are defined, <=> is automatically defined.

sawa
  • 165,429
  • 45
  • 277
  • 381