FAQ: Methods, Blocks, & Sorting - The Combined Comparison Operator

This community-built FAQ covers the “The Combined Comparison Operator” exercise from the lesson “Methods, Blocks, & Sorting”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise The Combined Comparison Operator

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’m confused about The Combined Comparison Operator <=>.

book_1 = “A Wrinkle in Time”
book_2 = “A Brief History of Time”
print book_1 <=> book_2

Output:
1

what I understand is the reason is showing 1 is because book _1 is greater than book_2. is it based on how many letters there are in each variable or is it based on alphabetical? It doesn’t seem correct to me

4 Likes

Yes, It’s like a dictionary. So if you observe these two strings.

“A Wrinkle in Time"
“A Brief History of Time”

Since both starts with letter ‘A’, they both are equal. Then it moves to second letter which is ‘W’ in first string and ‘B’ in the second string.

It will B <=> W and B comes before W. Hence the output 1.

12 Likes

That makes sense. Thank you :slight_smile:

I found that capitalized letters have a lower value than that of lower case ones.
So if we compared “A C A” (call it var_1) and “A c A” (var_2) with var_1 <=> var_2 it would return a -1.
And if we compare “a C a” and “a c b” var_1 <=> var_2 it would return -1.
So just be wary on how and when you use it.

3 Likes

not that it requires to be print or puts…

Indeed, when I printed my answer it told me I’d got it wrong.

1 Like

snap - straightforward as:
book_1 <=> book_2

I’m guessing that’s from ASCII or some other character encoding method which follows a similar pattern, because in ASCII capital letters go before lowercase letters and are therefore “earlier” and “greater” in the “alphabet”.

I’m following the comments that people are making here and understand the concept of having a hierarchy in the alphabet but I’m still confused.

The instructions ask you to compare (in this order):

book_1 = “A Wrinkle in Time”
book_2 = “A Brief History of Time”

book_1 <=> book_2

Based on this order and other people’s interpretations thus far, shouldn’t it be:

“W” or book_ 1 (the first operand) <=> “B” or book_2 (the second operand)

Since “W” (the first operand) is lower down on the alphabet than “B” (the second operand), it seems like the output should be -1, since the first operand is less than the second operand, that is, unless Ruby looks at letters farther down the alphabet as greater?

As does ASCII. A is 65, Z is 90. By using codes we are referring to ordinals, which we can see are ordered and sequential. Z does follow A so should have a greater ordinal, character code by any system of encoding.

3 Likes

Thank you! Your explanation makes sense.

1 Like

The combined comparison operator is interesting but I’m having a hard time grasping my head around using this operator on two strings, in our exercise, book_1 and book_2

Am I understanding this correctly in thinking the operator stops comparing once it arrives at the indices where the characters are no longer equal and either greater or less?

Thanks to anyone reading and is able to help.

Yes, exactly! It evaluates until there is a difference and then stops. Imagine you’re comparing two numbers 1234 and 1324. First the 1’s will be checked and are equal, then it will move to the second digits 2 is less than three so it will come before 1324, sequentially, no matter what those last two digits are.