What mean “.to_s”? My code was correct but it didn’t work till I wrote that. I don’t remember to read it in previous lessons, I’ve passed becouse I saw your comment so, thanks for that.
Here is my code (if it minds) ↓
puts "Enter your text here please: "
text = gets.chomp
text = gets.chomp
puts text
words = text.split()
frequencies = Hash.new(0)
words.each do |word|
puts frequencies[word] += 1
end
frequencies = frequencies.sort_by do |words, count| count end
frequencies.reverse!
frequencies.each do |word, count|
puts "#{word} #{count}"
end
.to_s means “to string”.
The variable f in the above code is an integer, and you can’t concatenate integers to strings (i.e. you can’t stick numbers to strings). For example
puts "hello " + “world” <— prints “hello world”
puts "hello " + 5 <— doesn’t work because 5 is not a string
puts "hello " + “5” <— prints “hello 5”
puts "hello " + 5.to_s <— prints “hello 5” (first converts 5 to “5”, then concatenates)