Iterating Over the Hash


#1

puts "whatever"
text = gets.chomp
words = text.split(" ")
puts "whenever"
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1}
frequencies = frequencies.sort_by {|a, b| b}
frequencies.reverse!
frequencies.each do |word, count|
puts word + " " + count.tos
end

i get this answer "Oops, try again. It looks like you aren't printing each key-value pair to the console correctly. Make sure to put a space between each key and value! "
can someone help please , i have no idea what i am doing wrong


#2

I think I found the problem.

puts word + " " + count.tos

should instead be:

puts word + " " + count.to_s

#3

thanks for your help but still it does not work
puts "whatever"
text = gets.chomp
words = text.split(" ")
puts "whenever"
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1}
frequencies = frequencies.sort_by {|a, b| b}
frequencies.reverse!
frequencies.each do |word, count|
puts word + " " + count.to_s
end

still get the same message, can u help ,me please?


#4

You're code functions on my browser. I would say refresh your browser a few times and try again. Otherwise I can't think of any reason it shouldn't work. I typed out character for character your code into my tab and it worked for me.


#5

It works well:

words = ['una', 'dos', 'tres', 'cuatro', 'cinco']

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


#6

Why it this hard :frowning: