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.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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.
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.
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?
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:
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.