Iterating Over the Hash


#1

I've been stuck with this for nearly a week. I can't figure out which line I have wrong.
The error message I've been getting is with syntax. can someone help me with which line has the error?

puts "Enter text"
text = gets.chomp
words = text.split
frequencies = Hash.new(0)
    words.each do |word| frequencies[word] += 1
    frequencies = frequencies.sort_by { |k, v| v }
        frequencies.reverse!
frequencies.each do |word, frequency|
    puts word + " " + frequency.to_s
end


#3

When written as a block, it is common to see on one line

words.each { |word| frequencies[word] += 1 }

but when written as a do..end, it is more common to see this...

words.each do |word|
    frequencies[word] += 1
end

#4

That's the code it's been accepting, but I'll try that. Thx :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.