6. Sorting the Hash


#1

I don't know what seems to be wrong with this code. Can anyone help?

puts "Enter text: "
text = gets.chomp

words = text.split

frequencies = Hash.new(0)
words.each do |word|
frequencies[word] += 1
end

frequencies.sort_by do |a, b|
b
end

frequencies.reverse!

Alternatively, this also doesn't work and returns the same error;

puts "Enter text: "
text = gets.chomp

words = text.split

frequencies = Hash.new(0)

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

frequencies.sort_by { |a, b| b }

frequencies.reverse!

The error is "undefined method `reverse!' for {"test"=>1, "text"=>1}:Hash"
and the website returned "Oops, try again. It looks like you haven't set the frequencies hash correctly. Check the first exercise if you need help!"


#2

Solved! I figured I forgot to re-assign the "frequencies" variable:

frequencies = frequencies.sort_by { |key, value| value }