Why should I use substitution operator when i sort the Hash


#1

my code is below

puts "input the text : "
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each do |word|
frequencies[word] += 1
end
frequencies = frequencies.sort_by do |word, frequency|
frequency
end
frequencies.reverse!

Here i have a question. I heard that ruby is object oriented language. Then i think i can sort the Hash without substitution operator, like

frequencies.sort_by do |word, frequency|
frequency
end

I think Hash is also object, therefore it has the "sort_by" method and I think it should work without substitution operator. But.... It's not work. :sob:
Is there anybody to help me understanding?


#2

Yes, it does work. The code your proposed sorts the hash according to the values in ascending order, then your reverse will give a hash in the descending order.