Ruby on Rails Saturday, September 12, 2015

On 11 September 2015 at 23:14, Calvin Fung <calvinfung22@gmail.com> wrote:
> 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 }

Does it help if it is written using the alternative form
words.each do |word|
frequencies[word] += 1
end

If not, which bit do you not understand?
1. words.each
2. do |word|
3. frequencies[word] += 1

Colin

--
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/CAL%3D0gLuqvkC1cp7JjXb7EizFVA3hG8xPtsdS5G2Jf0LzwQ9idQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment