Hey Calvin.
This line iterates through words array and counts how many times each word is there. Here how it works:
Let’s say you have words = ['never', 'say', 'never']. Your frequencies is an empty hash right now: {}.
In your line we start to iterate and take the first word never and we use it to access or create element in our hash: frequencies['never']. As we don’t have such an element in our hash yet, new element is created and initialized with 0 and then we add 1 to it. So after first iteration our frequencies hash looks like this: { 'never' => 1 }.
The same thing happens with ‘say’ word and frequencies now looks like this: { 'never' => 1, 'say' => 1 }.
And then we encounter ‘never’ again. There is such key in our hash so frequencies['never'] gets value (which is 1 now) and then adds 1 to it, so the resulting hash: { 'never' => 2, 'say' => 1 }.
Hope that helps.
Cheers,
Rem.
--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@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.