Ordering Your Library

I have a question about the “Ordering Your Library” lesson under “Blocks and Sorting” in the Learn Ruby course.
I’ve created the program, and I don’t get any return errors, but I don’t quite understand the “rev=false” and “if rev==true” bit.
Here’s my code:

def alphabetize(arr, rev=false)
arr.sort!
if rev == true
arr.reverse!
else
return arr
end
end
numbers = [3, 8, 2, 9, 4, 1]
puts alphabetize(numbers)

I understand that rev is, by default, false, and that the array “arr” will only be reversed if rev equals true. What I don’t understand is under what circumstances would rev equal true? What that would look like?

It would be true if set as such. The default is false, but a true in the argument would override that.

when you provide true as second argument to the function call. The parameter is only getting its default value if no second argument at function call

1 Like