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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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
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.
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.
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?
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.