Iterating Over the Hash


I've been stuck with this for nearly a week. I can't figure out which line I have wrong.
The error message I've been getting is with syntax. can someone help me with which line has the error?

puts "Enter text"
text = gets.chomp
words = text.split
frequencies =
    words.each do |word| frequencies[word] += 1
    frequencies = frequencies.sort_by { |k, v| v }
frequencies.each do |word, frequency|
    puts word + " " + frequency.to_s


When written as a block, it is common to see on one line

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

but when written as a do..end, it is more common to see this...

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


That's the code it's been accepting, but I'll try that. Thx :slight_smile:


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