0
0
Rubyprogramming~30 mins

Hash creation with symbols and strings in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Hash creation with symbols and strings
📖 Scenario: You are creating a simple contact list where each contact has a name and a phone number. You will use a Ruby hash to store this information.
🎯 Goal: Build a Ruby hash that uses both symbols and strings as keys to store contact names and their phone numbers.
📋 What You'll Learn
Create a hash with symbol keys and string keys
Use exact keys and values as specified
Access and print the hash content
💡 Why This Matters
🌍 Real World
Storing contact information in hashes is common in many Ruby applications like address books or user profiles.
💼 Career
Understanding how to use symbols and strings as hash keys is important for Ruby developers working with data structures and APIs.
Progress0 / 4 steps
1
Create a hash with symbol keys
Create a Ruby hash called contacts with these exact entries: :alice with value "123-4567", and :bob with value "987-6543".
Ruby
Need a hint?

Use the Ruby hash syntax with symbols as keys like alice: "123-4567".

2
Add string keys to the hash
Add two more entries to the contacts hash with string keys: "carol" with value "555-1234", and "dave" with value "555-5678".
Ruby
Need a hint?

Use square brackets to add string keys like contacts["carol"] = "555-1234".

3
Access and print all contacts
Use a contacts.each loop with variables key and value to iterate over the contacts hash and create a new hash called formatted_contacts where keys are strings (convert symbols to strings) and values are the phone numbers.
Ruby
Need a hint?

Use key.to_s to convert symbol keys to strings inside the loop.

4
Print the formatted contacts hash
Write puts formatted_contacts to display the formatted_contacts hash.
Ruby
Need a hint?

Use puts to print the hash. The output will show keys as strings.