Iterating over the Array


#1

Can't get code to work for this section! Error is: "It looks like you haven't added any key-value pairs to the frequencies hash."

puts "Put some text here"
words = gets.chomp.split (" ")
frequencies = Hash.new(0)
words.each do
|words| frequencies[words] += 1
end
frequencies.each do
| words, frequencies|
print words + " " + frequencies.to_s + " "
end


#2

Try with the parameter on the same line as do

words.each do |word|

#3

Same error! What the code looked like when I changed it:

puts "Put some text here"
words = gets.chomp.split (" ")
frequencies = Hash.new(0)
words.each do |words|
frequencies[words] += 1
end
frequencies.each do
| words, frequencies|
print words + " " + frequencies.to_s + " "
end


#4

It may not be advisable to chain the split() method to chomp.

text = gets.chomp
words = text.split        # default is split on white space

or

words = text.split(" ")   # note there is not white space in split()

We cannot use the list as parameter for each. We need a placeholder variable. Try,

words.each do |word|

So this will become,

frequencies[word] += 1