Iterating Over the Hash - undefined method?


The code I have written is practically the same as the one, which allows you to pass the exercise. Even though, after I have typed in my phrase, I keep getting this error message: “undefined method `[]’ for nil:NilClass”

This is my code:

puts "Write a text to analyze"
text = gets.chomp
words = text.split
frequencies =[0]
words.each do|word| frequencies[word] += 1
frequencies = frequencies.sort_by do |word, frequency|
frequencies.each do |word, frequency|
puts word + " " + frequency.to_s

What am I doing wrong, where is that wrong bit?
Thank you


You have a typo at frequencies =[0] line, it should be frequencies =

You can check the correct syntax on this link: class Hash.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.