4. Creating the Frequencies Hash


#1

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. "


#2

Something is missing here.


#3

text = gets.chomp

Thank you so much!


#4

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


#5

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


#6

instead of frequencies = Hash.new(0)

if we can use the literal notation method…

for example…

frequencies = { }


#7

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.


#8

Thanks again! :smiley: