4/7 Create a Histogram - Iterating Over the Hash


Stuck on the last part of this exercise. I keep getting the error
(Oops, try again. It looks like you aren't printing each key-value pair to the console correctly. Make sure to put a space between each key and value!

My code below:

puts "A phrase please:"
text = gets.chomp

words = text.split(" ")

frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {|a, b| b} 

frequencies.each { |word,frequency| puts
word + " " +frequency.to_s}

Thanks in advance :slightly_smiling:


I ended up being able to complete this section by submitting a blank form.
I still can't see what I was doing wrong, perhaps this is a bug with CA?


Are you stuck on lesson 4 or lesson 7?

Edit. I was able to reproduce the error (in lesson 7) using your code. The odd thing is it is not a bug in the SCT. For some reason Ruby did not like the last line.

frequencies.each { |word,frequency| puts word + " " + frequency.to_s }

All I did was add white space to the end, inside the brace. The space between + and frequency didn't matter, but it is cleaner.



Yes it was lesson 7.

Thanks for looking into this - much appreciated