Menu
Coddy logo textTech

Accessing Hash Elements

Part of the Fundamentals section of Coddy's Ruby journey — lesson 74 of 88.

Now that you know how to create hashes, you need to retrieve the values stored inside them. Unlike arrays where you use numeric indexes, hashes use keys to access their values.

Place the key inside square brackets to get its corresponding value:

person = {"name" => "Alice", "age" => 25, "city" => "Paris"}

puts person["name"]  # Outputs: Alice
puts person["age"]   # Outputs: 25

The key must match exactly what you used when creating the hash. If you try to access a key that doesn't exist, Ruby returns nil:

person = {"name" => "Alice", "age" => 25}

puts person["country"]  # Outputs: (nothing - nil)

You can also use the fetch method, which lets you provide a default value when the key is missing:

person = {"name" => "Alice"}

puts person.fetch("name", "Unknown")     # Outputs: Alice
puts person.fetch("country", "Unknown")  # Outputs: Unknown

This is useful when you want to handle missing keys gracefully instead of getting nil.

challenge icon

Challenge

Easy

Read four lines of input:

  1. A product name (string)
  2. A price (float)
  3. A quantity in stock (integer)
  4. A key to look up (string)

Create a hash called product with the following key-value pairs:

  • "name" mapped to the product name
  • "price" mapped to the price (as a float)
  • "stock" mapped to the quantity (as an integer)

Then, use the fetch method to retrieve the value for the key provided in the fourth input. If the key doesn't exist, use "Not found" as the default value. Print the result.

For example, if the inputs are Laptop, 999.99, 15, and price, the output should be:

999.99

If the inputs are Keyboard, 49.99, 100, and name, the output should be:

Keyboard

If the inputs are Mouse, 29.99, 50, and color, the output should be:

Not found

If the inputs are Monitor, 299.0, 8, and stock, the output should be:

8

Cheat sheet

To access values in a hash, use the key inside square brackets:

person = {"name" => "Alice", "age" => 25, "city" => "Paris"}

puts person["name"]  # Outputs: Alice
puts person["age"]   # Outputs: 25

If a key doesn't exist, Ruby returns nil:

person = {"name" => "Alice", "age" => 25}

puts person["country"]  # Outputs: (nothing - nil)

Use the fetch method to provide a default value for missing keys:

person = {"name" => "Alice"}

puts person.fetch("name", "Unknown")     # Outputs: Alice
puts person.fetch("country", "Unknown")  # Outputs: Unknown

Try it yourself

# Read input
product_name = gets.chomp
price = gets.chomp.to_f
quantity = gets.chomp.to_i
key_to_lookup = gets.chomp

# TODO: Write your code below
# Create a hash called 'product' with keys "name", "price", and "stock"
# Then use the fetch method to retrieve the value for key_to_lookup
# Use "Not found" as the default value if the key doesn't exist


# Output the result
puts result
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals