FAQ: Create a Histogram - Creating the Frequencies Hash

This community-built FAQ covers the “Creating the Frequencies Hash” exercise from the lesson “Create a Histogram”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Creating the Frequencies Hash

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

why does putsing a hash with a default value return {} instead of the default value?

I didn’t understand this lesson, can someone explain it further please?

The initial example differs slightly from what the exercise expects; the example’s puts allows us to see what is in the hash and what happens when we try to access a value.

“After pressing Run, enter a message in the console and hit enter to check your code” may be confusing since you must input text due to the text = gets.chomp from the previous exercise, but you aren’t required to puts out anything related to the hash to pass this exercise.

However, adding additional text similar to the example, as I’ve done below, can help reveal what is happening:

puts "Enter a phrase you'd like to analyze: "
# prints out whatever you typed

text = gets.chomp

words = text.split

frequencies = Hash.new(0)

puts frequencies
# {} prints because the hash is empty and contains no key-value pairs

puts frequencies["hello"]
# 0 prints because "hello" is a non-existent key
# So, we see the default value of 0

# Demonstrating the hash's behavior beyond this exercise:

frequencies[true] = "hello"

puts frequencies
# prints {true=>"hello"} and the value of 0 is gone!

frequencies[1] = "goodbye"

puts frequencies
# prints {true=>"hello", 1=>"goodbye"}

If a new key is added to the hash the exercise wants you to create, its default value will also be 0 unless you specifically assign it to something else. (I overwrote the default value in the code above to show how the hash changes for demonstrative purposes here, but note that it isn’t what the exercise is asking for.) If you didn’t set any default value for the hash, it would be nil.

0 is not printed to the console with puts frequencies because you’re not using a non-existent key to access the default value, so only the empty hash {} is shown.

The reason for why a default value of 0 is applicable for this exercise is explained in the lesson that follows this one:

Use .each to iterate over the words array. For each word we find, assume that the word itself is a key in frequencies and increment its value by 1. This is why our default is 0. The first time we find the word, it will have a default value of 0 that we can increment by 1.

More about the default value and Hash.new in the Ruby documentation.

1 Like