Could someone explain these Ruby functions to me?

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:


The Hash 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

green 1
red 2
blue 3

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

blue 3
green 1
red 2

because our method returns the keys (the colors) sorted by alphabetical order.


Remember that 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. :slight_smile:

1 Like