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 not an expert but the comparison operator <=> compares the value from =, > or <… so assuming none of the values are truely equal if you place the secondbook on the left of the comparison and the firstbook on the right it will force the function to list them in reverse order.
The | | brings the values into the method sort… although those details are fuzzy for me too so would be curious to hear more.
is referred to as the block parameters. These are local variables used by the block. We can use any names we wish.
Technically, combined comparison operator which is not a boolean operator, as such, but returns a numeric, -1, 0 or 1, depending how the LHS relates to the RHS.
How we order the block variables determines the order of the sort.
If you have an array of numbers it will sort in ascending order - no parameters given
So I ask because if you add parameters sort {|num1,num2| num2 <=> num1 } - I would assume in order for this to be able to work sort would already have to have the numbers from lowest to highest so that we know num1 <num2 before it is compared in num2 <=> num1 ?
In my mind just to clarify
What you have found is the default. It also disproves what I wrote above, and I stand corrected. From this standpoint it follows that in order to circumvent this, we will need the block params. I hope we’re putting the right edge on this.
Does |firstBook, secondBook| assign the books to a key and a value? or is it only comparing two books? I don’t fully understand how the whole list of books is sorted by the first and second book.
We are working specifically with an array, not a hash. The curly braces above are the block delimiters of the sort! method.
object.method { block }
They are known as block parameters, their names are arbitrary and only apply within the method block as iteration variables. They are not keys, since we are not dealing with a hash.
The real question is, do we want this object reversed, or do we want a copy, reversed? The bang operator is in-place so the original object is reversed.
I think the confusing thing here (for me anyway) is why pass block parameters "|secondBook, firstBook| " when you can just use books.sort.reverse and get the same answer?
The answer appears to be that for a simple array of integers or strings .sort or .sort.reverse would indeed suffice. Where the array contains someting more complex e.g. an employee record (where there are multiple pieces of data in each array element) then it’s not at all obviouse how the sort algorithm should work - in this case you would indeed need to specify this using block parameters. I think the use of the block paramaters in this exercise is there to build good habits going forwards.
In any sorting operation there are only ever two objects being compared. The names are only parameters, as in placeholders. The iteration process supplies the values from the iterable.