Code Doesn't Recognise .reverse! Method


#1



https://www.codecademy.com/courses/ruby-beginner-en-693PD/0/7?curriculum_id=5059f8619189a5000201fbcb#


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 = Hash.new(0)

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

frequencies.sort_by { |word, count| count }

frequencies.reverse!

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


#2

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.


#3

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