Code Doesn't Recognise .reverse! Method


I tried creating code according to the instructions, but when I try and call the .reverse! method on the frequencies hash I get this error message.

undefined method `reverse!' for {"yes"=>1, "no"=>3, "blah"=>2}:Hash

This is my code

puts "Please input some words:"
text = gets.chomp

words = text.split

frequencies =

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

frequencies.sort_by { |word, count| count }


frequencies.each do |word, count|
    puts word + " " + count.to_s


On the 8th line of code you basically ordered it to sort the frequency of the words by how many times each word occurred, and then didn't assign it into any variable. It should look like this:

frequencies = frequencies.sort_by { |word, count| count }

without that, it doesn't have a sorted array to reverse.


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