5. Iterating Over the Array can someone explain?


#1

I figured out the code for this exercise but I don't understand what the code does or what it means can someone explain this to me as I am kinda lost
puts "Enter text here?"
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each { |words| frequencies[words] +=1 } <== #this piece of code specifically


#2

Use a different block parameter.

plural.each { |singular| code }

as in,

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

#3

First line: You show the message to the user ""Enter the text here?"
Second line: You get input from the user.
3rd line: you split the input at spaces. Notice the quotation mark and the space inside it. The space is the delimiter. So it will split the sentence where it will see a space.
4th line: you create a hash with nil as a default value.
5th line: all the split words each of them you use as a key for the hash and increase their value from 0 (default) to 1.

frequency[your word as a key referring a key ] = frequency[your word as key referring a value which is the default 0] + 1

Always cross check with others.


#4

thank you for explaining I was mainly lost with words.each { |words| frequencies[words] +=1 } but I understand it now


#5

If you repeat a word the counter will go up too.
For example :the little mouse, the little house
frequency[1st little ] = [0 default] +1 = 1

for the second little:

frequency[2nd little] = [1 got from the previous assignment] +1 = 2