Ruby on Rails Friday, September 11, 2015

Hello there,

I am currently going through the Ruby course in Codeacademy and we are going over histograms. There is a particular exercise (5/8 of Data Structures, Meet Iteration) that is giving me trouble. I do not understand the line of code that is in bold below. I've gone through the exercise description a few times and looked around on the forums, but I don't understand what that line is doing. I was hoping someone could explain it to me in their own words - maybe that'll help?

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 }

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c249f5fa-f018-47b3-921c-564f43ed12ba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment