1

I need help for understanding - What is the difference between Equal to and Comparison?

Here is the case

    x == y means `Equal to`

    x = 10 and y = 10

    puts "X and Y are equal" if x == y

    puts "X and Y are equal" if x <=> y

I know when and where can I use equal to, but when and where can I use Comparison <=>

Thanks

3 Answers3

0

When you want to compare? It returns -1, 0, 1. For example, sorting users by first name:

users_by_first = users.sort { |u1, u2| u1.fname <=> u2.fname }

Here's another SO question asking about the spaceship operator.

Community
  • 1
  • 1
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Thanks for the link, Now I got a understanding when can I use <=> operator. Link is also very good. –  Aug 08 '12 at 18:19
0

<=> General comparison operator. Returns -1, 0, or +1, depending on whether its receiver is less than, equal to, or greater than its argument.

http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html

This is useful for sorting, for one thing.

JMM
  • 26,019
  • 3
  • 50
  • 55
0

Equality operators: == and !=

The == operator, also known as equality or double equal, will return true if both objects are equal and false if they are not.

"koan" == "koan" # Output: => true

The != operator, AKA inequality or bang-tilde, is the opposite of ==. It will return true if both objects are not equal and false if they are equal.

"koan" != "discursive thought" # Output: => true

Note that two arrays with the same elements in a different order are not equal, uppercase and lowercase versions of the same letter are not equal and so on.

When comparing numbers of different types (e.g., integer and float), if their numeric value is the same, == will return true.

2 == 2.0 # Output: => true

Comparison operators

Objects such as numbers and strings, which can be compared (amongst themselves) in terms of being greater or smaller than others, provide the <=> method, also known as the spaceship method. When comparing two objects, <=> returns -1 if the first object is lesser than the second (a < b), 0 in case they are equal (a == b) and 1 when the first object is greater than the second (a > b).

5 <=> 8  # Output:  => -1
5 <=> 5  # Output: => 0
8 <=> 5  # Output: => 1

Most comparable or sortable object classes, such as Integer, Float, Time and String, include a mixin called Comparable, which provides the following comparison operators: < (less than), <= (less than or equal), == (equal), > (greater than), >= (greater than or equal). These methods use the spaceship operator under the hood.

Comparison operators can be used in objects of all the above classes, as in the following examples.

# String
"a" < "b" # Output: => true
"a" > "b" # Output: => false

# Symbol
:a < :b  # Output: => true
:a > :b  # Output: => false

# Fixnum (subclass of Integer)
1 < 2  # Output: => true
2 >= 2  # Output: => true

# Float
1.0 < 2.0 # Output: => true
2.0 >= 2.0 # Output: => true

# Time
Time.local(2016, 5, 28) < Time.local(2016, 5, 29) # Output: => true

When comparing numbers of different classes, comparison operators will implicitly perform simple type conversions.

# Fixnum vs. Float
2 < 3.0  # Output: => true
2.0 > 3  # Output: => false

More info about Ruby operators is available at this blog post.

BrunoF
  • 3,239
  • 26
  • 39