4. Creating the Frequencies Hash


puts "A phrase please:"
text gets.chomp

words == text.split(" ")

frequencies = Hash.new(0)

This is what I have and it returns "It looks like frequencies isn't a hash. "


Something is missing here.


text = gets.chomp

Thank you so much!


for frequencies do we have to use the object notation? Can we use literal notation method? If so, please provide example. Thanks.


I don't understand your question. Are you asking if you can use a "hard coded" hash instead of using a variable behind it?


instead of frequencies = Hash.new(0)

if we can use the literal notation method…

for example…

frequencies = { }


No, because this syntax doesn't initialize the values to 0, so when you'll try to insert a key and increment its value by 1, it won't be able to (since it doesn't know its original value).

But if you're in a scenario where you don't want to increase the value but simply set it (like frequencies["yolo"] = 1), then you can use {} to declare your hash.


Thanks again! :smiley: