my problem here is that the console won’t compile because the sort.by function appears to be an undefined method
puts “enter a text”
text = gets.chomp
words = text.split (" ")
frequencie = Hash.new (0)
frequencies = frequencies.sort_by {|words, frequencies| frequencies}
frequencies.reverse!
anything wrong with my code?
@theinfrawolf
You are missing a line of code here
Not sure if you did last exercise but here’s a quick answer
words.each {|word| frequencies[word] += 1 }
Also there’s no space between split
and (
in this line
words = text.split (" ")
because it’s a function.
I know this is very late, but it might help someone else who has the same problem as you.
When you get the function not defined message, it is usually a sign that you made a typo. In this case, you created a hash called frequencie (notice the missing s). You then attempted to call .sort_by on frequencies which was never actually created, meaning it wasn’t a Hash, it was undefined.
If you change frequencie to frequencies you should be all set.