FAQ: Ordering Your Library - You Did It! (For Real This Time)

This community-built FAQ covers the “You Did It! (For Real This Time)” exercise from the lesson “Ordering Your Library”.

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

Learn Ruby

FAQs on the exercise You Did It! (For Real This Time)

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!

Hello,

I am fairly new to coding, and so far, it went well.
But now I am a bit stuck, So I was creating my alphabetize method like this :

def alphabetize(arr, rev=false )
  arr.sort!
  if rev == true
    arr.reverse!
  else
    return arr
  end
end

numbers = [2, 6, 9,1, 58, 4]

puts alphabetize (numbers)

this works well, but I wanted to see what happened if I gave the method “true” as a second parameter when called
And that’s where it went wrong…

Can someone explain to me why this doesn’t work ?

def alphabetize(arr, rev=false )
  arr.sort!
  if rev == true
    arr.reverse!
  else
    return arr
  end
end

numbers = [2, 6, 9,1, 58, 4]

puts alphabetize (numbers, true)

If the second parameter is not empty, I just wanted to test if it would go to my “else statement”.

thank’s for your help :slight_smile:

Sarah

Remove the blank space between the function name and the arguments so that instead of
puts alphabetize (numbers, true)
you should have
puts alphabetize(numbers, true)
That should work.

2 Likes

Oh yes, this works! :smiley:

thank you !!! I feel somewhat dumb now.

I will make sure to watch out for that kind of mistake! :sweat_smile:

And sorry for the late reply !!!

Is there a reason why it was necessary to create this method instead of using the built-in method of .reverse ?

Hi everyone!

I’m still completely lost with the Combined Comparison Operator.

What am I missing here? Why can’t I just use my code on lines 14-18 instead of all the code above?

Also, could someone please explain what rev=false is doing. I don’t get it :confused:

Thanks.

Though you could chain sort! and reverse together, building this method helps reinforce and practice what the course has been teaching about methods and if/else. You could also make the method more complex and then would only have to type alphabetize (or whatever you called your method) to call it wherever you like without having to manually write out every method you wanted to chain together; alphebetize is also nicely descriptive of what the method is doing.

rev=false means that we don’t want to reverse the array; if you set rev to true, then you do want to reverse the array. First, test out how rev=false is working by using some code from the previous exercise with an array of numbers:

def alphabetize(arr, rev = false)
  arr.sort!
  if rev == true
    arr.reverse!
  else
  	arr
  end
end

numbers = [3, 5, 1, 6]

puts alphabetize(numbers) # => [1, 3, 5, 6]

# If you run the same code after setting rev=true, the result is...
# => [6, 5, 3, 1]

Now, let’s test it out on the array of books:

def alphabetize(arr, rev=false)
  if rev
    arr.sort { |item1, item2| item2 <=> item1 }
  else
    arr.sort { |item1, item2| item1 <=> item2 }
  end
end

books = ["Heart of Darkness", "Code Complete", "The Lorax", "The Prophet", "Absalom, Absalom!"]

puts "A-Z: #{alphabetize(books)}"
# => ["Absalom, Absalom!", "Code Complete", "Heart of Darkness", "The Lorax", "The Prophet"
puts "Z-A: #{alphabetize(books, true)}"
# => ["The Prophet", "The Lorax", "Heart of Darkness", "Code Complete", "Absalom, Absalom!"]
# Because we're setting rev to true here, the array is reversed.

As I mentioned in my comment above, you could use sort! and reverse separately or chain them together, but the intention with this series of lessons seems to be to explain and reinforce how to create a method.