这是下面的正确答案,但我不明白为什么可以在最后一行输入“puts word”,而输入“puts x”是不可接受的,因为它的定义方式几乎完全相同
puts "Text please "
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new{0}
words.each { |word| frequencies[word] += 1}
frequencies = frequencies.sort_by { |x, y| y }
frequencies.reverse!
frequencies.each do |word, frequency|
puts word + " " + frequency.to_s
end