Menu
Coddy logo textTech

Symbols as Hash Keys

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

So far, you've been using strings as hash keys with the hash rocket syntax. However, Ruby developers commonly use symbols as keys instead, which offers a cleaner and more efficient approach.

Remember symbols from earlier? They're lightweight, immutable identifiers that start with a colon. When using symbols as keys, Ruby provides a special shorthand syntax:

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

puts person  # Outputs: {:name=>"Alice", :age=>25, :city=>"Paris"}

Notice how name: replaces :name =>. This shorthand only works when the key is a symbol. To access values, use the symbol with square brackets:

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

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

Adding and modifying entries works the same way:

person = {name: "Alice"}

person[:city] = "Paris"
person[:name] = "Bob"

puts person  # Outputs: {:name=>"Bob", :city=>"Paris"}

Symbols are preferred as hash keys because they're faster to compare than strings and use less memory. You'll see this pattern throughout Ruby code, so it's worth getting comfortable with it.

challenge icon

Challenge

Easy

Read three lines of input:

  1. A car brand (string)
  2. A model name (string)
  3. A year (integer)

Create a hash called car using the symbol shorthand syntax (e.g., key: value) with the following key-value pairs:

  • :brand mapped to the car brand
  • :model mapped to the model name
  • :year mapped to the year (as an integer)

Then perform these operations:

  1. Print the value of the :model key
  2. Add a new key :color with the value "black"
  3. Update the :year key by adding 1 to its current value
  4. Print the final hash

For example, if the inputs are Toyota, Camry, and 2020, the output should be:

Camry
{:brand=>"Toyota", :model=>"Camry", :year=>2021, :color=>"black"}

If the inputs are Honda, Civic, and 2018, the output should be:

Civic
{:brand=>"Honda", :model=>"Civic", :year=>2019, :color=>"black"}

If the inputs are Ford, Mustang, and 2023, the output should be:

Mustang
{:brand=>"Ford", :model=>"Mustang", :year=>2024, :color=>"black"}

Cheat sheet

Ruby provides a shorthand syntax for creating hashes with symbol keys:

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

This is equivalent to using the hash rocket syntax with symbols:

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

To access values from a hash with symbol keys, use square brackets with the symbol:

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

Adding and modifying entries works the same way as with string keys:

person[:city] = "Paris"  # Add new key-value pair
person[:name] = "Bob"    # Modify existing value

Symbols are preferred as hash keys because they're faster to compare than strings and use less memory.

Try it yourself

# Read input
brand = gets.chomp
model = gets.chomp
year = gets.chomp.to_i

# TODO: Write your code below
# 1. Create a hash called 'car' using symbol shorthand syntax with :brand, :model, and :year keys

# 2. Print the value of the :model key

# 3. Add a new key :color with the value "black"

# 4. Update the :year key by adding 1 to its current value

# 5. Print the final hash
quiz iconTest yourself

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

All lessons in Fundamentals