Learn
Create a Histogram

Creating the Frequencies Hash

Good! Now we'll start counting words using a hash.

We'll want to make sure the hash has a default value.

h = Hash.new("nothing here")

puts h
# {}

puts h["kitty"]
# nothing here
  1. In the example above, we create a new, empty hash h that has a default value of "nothing here".
  2. Then we print out {}, the value of h, just to show that h really is empty.
  3. Then we print out nothing here as we try to access the value stored by the key "kitty".

If you have a hash with a default value, and you try to access a non-existent key, you get that default value.

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.