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
end

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.

2 Likes

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.

6 Likes

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.

2 Likes

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.

2 Likes

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.

Hello! I’m not completely clear on why I need to use curly braces on Step 5 of Create a Histogram. In order to increment the frequency of each word by 1, I need to use this code:

words.each {|word| frequencies[word] += 1}

I am used to using curly braces in Javascript but earlier lessons in the Ruby course suggested that we don’t need them all the time. Is there any difference / benefit to using the following code?

words.each do |word| frequencies[word] += 1 end

Thanks!

I’m new to programming and maybe I’m dumb but it took me a while to figure out what exactly we’re doing here:

words.each { |word| frequencies[word] += 1 }

For each word in the array words, we are taking that word and adding it as a key to the hash frequencies and assigning it a value of +1.

I think it would be beneficial to other newbies if this was explained in this section.

What I still don’t understand is what this accomplishes. Say we have user input “I went to the shop to get bread”. We run the .each method on it and we get the resulting hash:

frequencies = {
 "I" => 1
 "went" => 1
 "to" => 1
 "the" => 1
 "shop" => 1
 "to" => 1
 "get" => 1
 "bread" => 1
}

Or do we get a value of 2 for “to”? But if so, how/why? Does the .each method treat word[2] and word[5] as the same entity? Why?

3 Likes

Be sure your frequencies hash has be initialized to default value of zero.

frequencies = Hash.new(0)

Each word is treated separately. If it is not in the hash, a new key is created and given an initial value of zero, to which we can now add 1 with this and any more occurrences. There should be a 2 for ‘to’.

Hi there, when I click Run there is only one, first line of code : "Enter a phrase you’d like to analyze: " and that’s it. Nothing happend after i put some text in it. It’s happend to me before with few exercises. There is no show of result.

Can you upload a screenshot showing a) your code, b) the terminal and c) the error/feedback message that pops up at the bottom? (There is an upload button in the forum post editor)

Try the following:

  • Click the run button (The button will start spinning. Ignore it)
  • Without waiting too long, click once or twice with your mouse on the terminal window. This will highlight the terminal. Unfortunately, it seems that the cursor doesn’t start blinking in this particular exercise. Ignore that. Just start typing your phrase. You should see the words appear in the terminal as you type. Once you are done, press the enter button on your keyboard.
  • The run button should now stop spinning and the Next button should allow you to proceed to the next exercise.

For the “Iterating Over the Array” exercise, you just need to iterate over the words array using .each

The puts statement will be specified in a later exercise titled “Iterating Over the Hash”. Once you complete that exercise, you should see the output in the terminal.