Hey. I completed the excercise but it would be great if someone could clear some things up for me.
print "This is sooooo eaaaasyyyy." text = gets.chomp # lets assume I typed "uno dos dos tres tres tres" puts text words = text.split frequencies = Hash.new (0) words.each do |word| frequencies [word] += 1 end
Firstly, some suggest adding (" ") after the text.split, yet it does not seem to matter.
Secondly, about the hashes. The default value is 0 and as I understand this "0" is assigned when a non-existant key is requested.
"uno" exists and has been typed once, so its value before iteration should be "1", and after iteration "2", no? Instead, after iteration it is 1.
Thirdly, how does the user input suddently, from an Array, change to being a part of the Hash in this code?
words.each do |word| frequencies [word] += 1 end
Is it that "frequencies [word] +=1" both iterates and sorta converts the data-type at the same type? Before that, frequencies was just an empty Hash.