📖 Scenario: You are building a simple contact list app. Each contact has a name and a phone number. You want to store this information in a Ruby hash.
🎯 Goal: Learn how to create hashes with symbol keys and string keys, and decide which one to use for your contact list.
📋 What You'll Learn
Create a hash with string keys for name and phone
Create a hash with symbol keys for name and phone
Compare accessing values using string keys and symbol keys
Print the results to see the difference
💡 Why This Matters
🌍 Real World
Storing and accessing data in hashes is common in Ruby apps like contact lists, settings, and configurations.
💼 Career
Understanding symbol vs string keys helps write efficient and clear Ruby code, a valuable skill for Ruby developers.
Progress0 / 4 steps
1
Create a hash with string keys
Create a hash called contact_string_keys with these exact entries: 'name' => 'Alice' and 'phone' => '123-456-7890'.
Ruby
Hint
Use curly braces {} to create a hash. Use strings as keys with the => symbol.
2
Create a hash with symbol keys
Create a hash called contact_symbol_keys with these exact entries: name: 'Alice' and phone: '123-456-7890'.
Ruby
Hint
Use symbols as keys by writing the key name followed by a colon, like name:.
3
Access values using string and symbol keys
Write two lines: one to get the name from contact_string_keys using the string key 'name', and one to get the name from contact_symbol_keys using the symbol key :name. Store them in variables name_from_string and name_from_symbol respectively.
Ruby
Hint
Use square brackets [] with the exact key to get the value from a hash.
4
Print the results
Print the values of name_from_string and name_from_symbol on separate lines using puts.
Ruby
Hint
Use puts to print each variable on its own line.
Practice
(1/5)
1. Which key type is best to use in a Ruby hash when the keys are fixed and known in advance?
easy
A. Symbols, because they are faster and use less memory.
B. Strings, because they are easier to type.
C. Integers, because they are numeric.
D. Arrays, because they can hold multiple values.
Solution
Step 1: Understand key types in Ruby hashes
Symbols are immutable and unique, making them efficient for fixed keys.
Step 2: Compare symbols and strings for fixed keys
Symbols use less memory and are faster than strings when keys do not change.
Final Answer:
Symbols, because they are faster and use less memory. -> Option A
Quick Check:
Fixed keys = Symbols [OK]
Hint: Use symbols for fixed keys to save memory and speed [OK]
Common Mistakes:
Using strings for fixed keys wastes memory
Confusing symbols with strings
Thinking integers or arrays are valid hash keys here
2. Which of the following is the correct NEW STYLE syntax to define a Ruby hash with symbol keys?
easy
A. { 'name' => 'Alice', 'age' => 30 }
B. { name: 'Alice', age: 30 }
C. { :name => 'Alice', :age => 30 }
D. { name => 'Alice', age => 30 }
Solution
Step 1: Recognize Ruby symbol key syntax
Ruby allows symbol keys with the syntax key: value (new style) or :key => value (old style).
Step 2: Identify the correct syntax among options
{ name: 'Alice', age: 30 } uses the modern symbol key syntax: name: 'Alice', age: 30.
Final Answer:
{ name: 'Alice', age: 30 } -> Option B
Quick Check:
Symbol keys use key: value syntax [OK]
Hint: Use key: value for symbol keys in Ruby hashes [OK]