I tried to formulate the questions in the screenshots. Hopefully you get what I mean
1:“why is this needed?”
2:“why must we convert it to a string?”
It’s this lesson in case anyone was wondering: https://www.codecademy.com/courses/learn-ruby/lessons/create-a-histogram/exercises/what-youll-be-building-3
colors contains key / value pairs of colors and their count.
We want to sort these pairs by count, which will be sorted from the smallest to the largest.
So the result of sorting by count is
as expected, we sorted them from smallest to largest. But we’d prefer to have them sorted by largest count, so we simply reverse the array order.
If we sorted them by color, the result would be
because our method returns the keys (the colors) sorted by alphabetical order.
puts is short for “put string”.
name is a string (because “apple”, “banana” and “cherry” are all strings),
" " also is a string, however count isn’t. 2, 3 and 5 are Integers.
If you attempted to
puts without converting count.
to_s, you’d receive back a TypeError along the lines of “no implicit conversion of Integer into String”.
thanks a lot! i got it now.