0
0
Rubyprogramming~15 mins

Accessing and setting values in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing and setting values
📖 Scenario: You are managing a small store's inventory using a Ruby program. You need to keep track of the quantity of different fruits available.
🎯 Goal: Build a Ruby program that creates a hash with fruit quantities, updates the quantity of one fruit, and then prints the updated quantity.
📋 What You'll Learn
Create a hash named inventory with exact keys and values
Create a variable named fruit_to_update with the exact string value
Update the quantity of the fruit in the inventory hash using the variable
Print the updated quantity of the fruit using puts
💡 Why This Matters
🌍 Real World
Managing inventory quantities in a store or warehouse is a common task. Using hashes to store and update product counts helps keep track of stock easily.
💼 Career
Many programming jobs require working with data structures like hashes or dictionaries to manage and update information efficiently.
Progress0 / 4 steps
1
Create the inventory hash
Create a hash called inventory with these exact entries: 'apple' => 10, 'banana' => 5, 'orange' => 8
Ruby
Need a hint?

Use curly braces {} to create a hash and separate keys and values with =>.

2
Set the fruit to update
Create a variable called fruit_to_update and set it to the string 'banana'
Ruby
Need a hint?

Use = to assign the string 'banana' to the variable fruit_to_update.

3
Update the quantity of the selected fruit
Increase the quantity of the fruit stored in fruit_to_update by 3 in the inventory hash
Ruby
Need a hint?

Use inventory[fruit_to_update] to access the value and add 3 to it.

4
Print the updated quantity
Use puts to print the updated quantity of the fruit stored in fruit_to_update from the inventory hash
Ruby
Need a hint?

Use puts inventory[fruit_to_update] to print the updated quantity.