'frequency isn't a hash' - Why is this code not working?


#1

puts "Text please: "
text = gets.chomp

words = text.split(" ")
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {|a, b| b }
frequencies.reverse!
frequencies.each { |word, frequency| puts word + " " + frequency.to_s }

this is my code for the task, however when I went to submit it, it gave me the response that 'Frequency isn't a hash', how is this incorrect?


#2

I tried your code on the exercise 7/8 and it passes. If it's not working on your side it might be a problem with your browser, try refreshing the page or emptying the cache of your browser.


#3

I'll give it a shot, thanks for the suggestion


#4

This doesn't work for me. I tried Firefox, Chrome, & Explorer. None of the browsers make a difference. It still give me the error of, "Frequency isn't a hash!" HEELPPPP


#5

It is not a hash because in the previous problem when we sorted the different words, the method sort_by changes a hash to a matrix. Do this

puts "Please enter text"
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each {|word| frequencies[word] +=1 }
frequencies1 = frequencies.sort_by {|palabra, count| count}
frequencies1.reverse!
frequencies.each {|x,y| puts x + " " +y.to_s

See how I changed the name to frequencies1


#6

Ok, i've been through it and figured it out now. Thank you for the suggestions and assistance


#7

I have to ask the experts. what on earth does this code do:

words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {|a, b| b }

I cannot work out the logic of this. Is frequencies now a key or a value?
What is the logic behind {|a,b| b}

Thanks in advance folks!