What i'd like to do is push a bunch of user inputted strings, separated by their spaces to Hash Keys and assign each of those Keys a numerical value from 0 upwards in their order of input. For example;
User input: "a s d f b"
Result:
a => 0
b => 1
c => 2
d => 3
f => 4
b => 5
But my code is only outputting 0 with no increment, please see my code below;
puts "Please enter data:"
text = gets.chomp
words = text.split(" ")
    frequencies = Hash.new(0)
words.each do |k|
    frequencies[k] += 1
end
frequencies.each do |x,y|
    puts x + " " + y.to_s
end
Can anyone see what's wrong with the code above?
 
                        
The default value of every key in this
Hashis0, so byYou are incrementing every value (of non-existing key) from
0to1.A simple fix is to use an external counter as the value: