0
0
Rubyprogramming~15 mins

Nil as the absence of value in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Nil as the absence of value
📖 Scenario: Imagine you are managing a list of people and their favorite colors. Sometimes, a person might not have a favorite color yet. In Ruby, nil is used to show that something has no value.
🎯 Goal: You will create a Ruby program that uses nil to represent missing favorite colors and then check for those missing values.
📋 What You'll Learn
Create a hash called people_colors with exact keys and values
Use nil to represent no favorite color
Create a variable called missing_color_count to count how many people have no favorite color
Use a for loop with variables person and color to iterate over people_colors
Print the number of people with no favorite color using puts
💡 Why This Matters
🌍 Real World
In real programs, <code>nil</code> helps show when information is missing or unknown, like a missing phone number or address.
💼 Career
Understanding <code>nil</code> is important for Ruby developers to handle data correctly and avoid errors when values are missing.
Progress0 / 4 steps
1
Create the people_colors hash
Create a hash called people_colors with these exact entries: 'Alice' => 'blue', 'Bob' => nil, 'Charlie' => 'green', 'Diana' => nil
Ruby
Need a hint?

Use curly braces {} to create a hash. Use nil for missing favorite colors.

2
Create a counter for missing colors
Create a variable called missing_color_count and set it to 0 to count people with no favorite color
Ruby
Need a hint?

Just set missing_color_count to zero to start counting.

3
Count how many people have no favorite color
Use a for loop with variables person and color to iterate over people_colors. Inside the loop, increase missing_color_count by 1 if color is nil
Ruby
Need a hint?

Use color.nil? to check if the color is missing.

4
Print the count of missing favorite colors
Use puts to print the value of missing_color_count
Ruby
Need a hint?

Use puts missing_color_count to show the number of people without a favorite color.