FAQ: Create a Histogram - Iterating Over the Array

This community-built FAQ covers the “Iterating Over the Array” exercise from the lesson “Create a Histogram”.

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

Learn Ruby

FAQs on the exercise Iterating Over the Array

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!

How is words an array? I am confused. I thought the array was frequencies. Thank you.

words is an array by definition…

words = text.split

The sort_by method returns an array of arrays. The frequencies variable is re-used and takes on that array.

I wrote the following code, trying to see the incrementing pair from the words array split:

puts "Type Something: "
text = gets.chomp
words = text.split
frequencies = Hash.new(0)
words.each do | word |
frequencies[word] += 1

puts frequencies

however, the outcome assigns all words to 1 and I can’t see why it’s not incrementing:

*Type Something: *
not incrementing stays as 1 # the sentence i wrote
{“not”=>1, “incrementing”=>1, “stays”=>1, “as”=>1, “1”=>1}

As you can see, each word in the sentence: not incrementing stays as 1 is assigned a value of 1, not 1, 2, 3, 4 and 5.

Any idea? Many thanks.

They all get set to 1 when each key is updated (incremented). There are no multiples of any one or more words so their frequencies are all 1.

Try a sentence that has repeated words and see what the outcome is.

1 Like

Finally figured this out myself. Think this section is poorly worded for the newbie. The sentence in question is:

This is why our default is 0 . The first time we find the word, it will have a default value of `` 0 that we can increment by 1`.

So, the number after the word, relates to the number of times each word appears in the sentence.

They should add here:

The second time we find the same word, it will have a value of 2.

This would be much much clearer.


Oh wow, I just saw you had replied after i wrote that. Thanks for being so quick.

1 Like

Hi there,
I’ve found many bugs in the exercises for Ruby and have now become incredibly frustrated by the frequency of the bugs. In this exercise it asks for the .each to be used on words along with incrementation. After finally getting the solution => words.each { |word| frequencies[word] += 1 } <=
I was told I don’t have a key => value pair. Not realising how I’ve made a mistake, I gave in and pressed the “Give me the solution” button, to see that my solution was the correct one! Could someone explain why it wouldn’t let me go with my solution and why there are so many bugs regarding the solutions as I’ve started doubting myself and my abilities, to realise it’s an issue with the exercises.

1 Like

Hey @fazzy15, welcome to the forums!

I understand this can be incredibly irritating, but unfortunately there isn’t much we can do. I would recommend submitting a bug report so the people who can make a difference receive your feedback.

My every attempt hangs, so I tried the solution, copied and pasted it in, and that hangs too. Is there any working solution?

Also, is there any way to break a loop that hangs?

I sometimes refresh the page and it then goes through fine.

Here’s what I’m not clear about. Suppose a word appears more than once. The when we’re looping on each, and get to the second appearance of the word, shouldn’t we try to create a new (second) hash entry for the word in frequencies, resulting in an error? Shouldn’t we have to do some sort of search each loop through each to check if the word already has a hash entry; then if yes, add 1 to its frequencies count, and if no, then create a new hash-table entry for the word and set its count to 1?

If the hash already exists, Ruby will add 1 to its value, not try to create a new hash. It will only create a new hash if one does not already exist. That hash will have an initial value of zero, to which the 1 can be added.

Thanks. In most other languages, you’d probably have to search the hash to see if the entry already existed, and locate the entry before adding one

The fact that you don’t in Ruby is a cool feature

Given that we can define a default initial hash. Gotta love that. I wish I was a real Ruby user. It’s an amazing language.