# FAQ: Create a Histogram - Iterating Over the Hash

This community-built FAQ covers the “Iterating Over the Hash”.

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

## FAQs on the exercise Iterating Over the Hash

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.

Agree with a comment or answer? Like () to up-vote the contribution!

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!

Hi, why do we have to turn “frequency” into a string before printing the number? Is it not just the number we are printing?

1 Like

There is an issue with part 7 of this exercise. The instructions say:

Iterate over `.each` key/value pair in the `frequencies` hash.

However, since we’ve used `sort_by` earlier in the code, we’re actually iterating over a two-dimensional array, not a hash.

2 Likes

To be fair, it is a one dimension array of tuples, which are still key-value pairs.

2 Likes

@coresurfer30453: Agreed. It’s incorrect for the exercise to say “the `frequencies` hash”, since it’s no longer a hash. And the exercise’s title (“Iterating Over the Hash”) is misleading too, since we’re actually iterating over an array.

In the example given at the top of the exercise, `fruit` is indeed a hash, and so in this line:

``````fruit.each do |name, count|
``````

we are indeed obtaining the key (`name`) and value (`count`) of each item in the hash.

But since `frequencies` is now an array of tuples, then in:

``````frequencies.each do |word, frequency|
``````

the block parameters actually contain the 1st and 2nd elements of each tuple.

It just so happens in this case that each tuple has 2 elements, but in other situations the tuples may have a different quantity of elements (eg: `array_of_tuples.each do |a, b, c|` for tuples with 3 elements).

The previous exercise (“Sorting the Hash”) mentions:

Just so you know, the `.sort_by` function returns an array of arrays, but that’s fine for our purposes.

so the student knows the `frequencies` variable now contains a sorted array. So I think it’s misleading and confusing for the exercise to then expect the student to write:

``````frequencies.each do |word, frequency|
``````

(by copying the given `fruit.each` example), because the course hasn’t yet taught the student that `.each` (on an array) can receive multiple block parameters. The course has only taught the usage of a single block parameter containing the current `.each` array element (see the “Iterating Over Arrays” exercise in the “Arrays and Hashes: Data Structures” lesson).

4 Likes

Yep, that confused me too.

I was aware that `array.each` can take a single parameter and provided a working solution but it wasn’t accepted:

``````frequencies.each do |pair|
puts "#{pair[0]}  #{pair[1]}"
end
``````

I was glad to see your comment here and I now understand how arrays can get multiple parameters but I’ve learned it here… not in the exercise

Codecademy, maybe you can update the wording in the exercise?

4 Likes

You can try to remove the `.to_s` but it would throw an error (if you try it in IRB):

`TypeError: no implicit conversion of Fixnum into String`

The same would happen if you tried to do : `"1" + 1` .

What would you expect as a result here? Should you get `"11"` or `2`?

Well, since you’re starting with a string, Ruby assumes you want a string and treats the `+` as a concatenation mechanism. However, it walks right into an Integer and simply refuses to convert it automatically to a string, stops the concatenation and throws the error above.

So… if you do want the string you’ll have to convert the number to a string yourself.

`"1" + 1 ==> Error`
`"1" + 1.to_s ==> "11"`
`"1".to_i + 1 ==> 2`

In the last example, we convert the first `1` into an integer so the `+` for Ruby is addition, not concatenation, that’s why we get `2` as a result.

2 Likes

Well, I’m not sure where the issue is, but you can make it work like so:

``````frequencies.each do |pair|
puts pair[0] + " " + pair[1].to_s
end

``````

Probably they check for the use of .to_s, other than that I’m not sure

Since `frequencies` is an array of key, value tuples, we can unpack each tuple in the block parameter:

``````frequencies.each { |k, v| puts "#{k} #{v}" }
``````

Because we use string interpolation, the values are cast to string so no conversion needed on our part.

1 Like

Thanks for this. I was really confused about the wording since it did tell us that frequencies was now an array and not a hash.

I found this exercise very confusing. I think what made it worse was that I used the view solution option and their own solution doesn’t even work and results in an “execution expired” message every time. Rather frustrating.

4 Likes

Each one of my attempted solutions allowed me to input text, but after pressing Enter it would just loop until it said “execution expired”. I asked for the solution and it did the same thing. I’m wondering if there might be a bug?

1 Like

Hello, So i have been working through this one the third time, I don’t want to check the answer and change my code. The system shows this as correct, but I get the error message “No implicit conversion of string to integer.”
I was hoping for some insight. Count is the only object I can see that would need to be converted to an integer. but count.to_i does not change the error message.

puts “Please enter some text here:”
text = gets.chomp

words = text.split(" ")
frequencies = Hash.new(0)
words.each do |word|
frequencies[word] += 1
frequencies = frequencies.sort_by do |word, count|
frequencies.each do |word, count|
end
puts word + " " + count.to_s
end
frequencies.reverse!
end

What is the each loop supposed to be doing? Looks like you have two loops chained together.

``````puts word + " " + count.to_s
``````

can be interpolated…

``````puts "#{word} #{count.to_s}"
``````

but it should be inside the loop body.

it looks like this lesson 7 isn’t accepting the codes. Like others mentioned, the terminal shows “execution expired” and in the script section, it shows “Your code doesn’t look quite right. Check the Hint if you need help!”. Even it’s own provided solution doesn’t work. Looks like something is wrong, but certainly not the code. Even if we apply the provided solution, it still shows execution expired.

Whenever gets.chomp is involved, the code does not work and stops at execution expired. Even if the code is correctly written. Does the same with solution. The way gets.chomp is executed is the issue.

So this is merely a way of writing it with the + since same variable type is needed to use the +?
Because
puts word + " " + “#{count}”
also works but I assume it also just turns the variable into a string (?)

Why not just use the interpolation? Above is a mix that makes very little sense.

``````puts "#{word} #{count}"
``````

Interpolation does not alter the value of the variable, but makes a copy and casts it to string (if it is not already one). `count` will still be a numeric.

1 Like

I am having the same problem a everyone else. My code doesn’t work, the code provvided in the “Solution” section doesn’t work. “Execution expired”. No way to proceed.

I can not proceed either. I had the same issue with the code the exercise before this one. I wrote it exactly how it says in the solution, then i left my screen on, and left my computer.

Next thing i knew, came back and it was onto the next exercise, which is this one. And i wrote out the code, and i am getting nothing but…

And…