Undefined method `reverse!


#1



all my code is correct but it give me following error
 undefined method `reverse!' for {"helh"=>1, "sdh"=>1, "ljhue"=>1}:Hash
the code is here:
puts "Enter your text"
text=gets.chomp
words=text.split
frequencies=Hash.new(0)

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

frequencies.sort_by do |word,count|
    count
end
frequencies.reverse!


#2

The reason you are receiving that error is because of this code,

the .sort_by method doesn;t require an end statement or a count line.

It should be on one line only,

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


#3

When using sort_by, do I always have to store it? I mean is this part necessary: frequencies = , or is there another way to use sort_by?


#4

Yes you always have to store it. :slight_smile:


#5

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