📖 Scenario: Imagine you are organizing a list of tasks for a small project. Each task has a name and a status. You want to use symbols to represent the task names because symbols are efficient and immutable, meaning they cannot be changed once created.
🎯 Goal: You will create a hash with task names as symbols and their statuses as strings. Then, you will add a new task symbol and finally print all tasks with their statuses.
📋 What You'll Learn
Create a hash called tasks with three tasks using symbols as keys: :design, :development, and :testing with statuses 'done', 'in progress', and 'pending' respectively.
Create a symbol variable called new_task and assign it the symbol :deployment.
Add the new_task symbol as a key to the tasks hash with the status 'not started'.
Print the tasks hash to show all tasks and their statuses.
💡 Why This Matters
🌍 Real World
Symbols are often used in Ruby programs to represent fixed names or identifiers, such as keys in hashes, because they use less memory and are faster to compare than strings.
💼 Career
Understanding symbols and their immutability is important for Ruby developers, especially when working with frameworks like Ruby on Rails where symbols are used extensively for keys, method names, and options.
Progress0 / 4 steps
1
Create the initial tasks hash with symbols
Create a hash called tasks with these exact entries: :design => 'done', :development => 'in progress', and :testing => 'pending'.
Ruby
Hint
Use the syntax symbol: value to create symbol keys in the hash.
2
Create a symbol variable for a new task
Create a variable called new_task and assign it the symbol :deployment.
Ruby
Hint
Use the colon : before the word to create a symbol.
3
Add the new task symbol to the tasks hash
Add the new_task symbol as a key to the tasks hash with the value 'not started'.
Ruby
Hint
Use square brackets [] to add a new key-value pair to the hash.
4
Print the tasks hash
Write puts tasks to print the tasks hash and show all tasks with their statuses.
Ruby
Hint
Use puts to display the hash on the screen.
Practice
(1/5)
1.
What is a key characteristic of a Ruby Symbol?
easy
A. It is immutable and unique for the same name
B. It can be changed after creation
C. It stores large amounts of data
D. It behaves like a string with spaces
Solution
Step 1: Understand what a Symbol is
A Symbol in Ruby is a name or label that is immutable, meaning it cannot be changed once created.
Step 2: Recognize uniqueness of Symbols
Two Symbols with the same name always refer to the same object, making them unique and memory efficient.
Final Answer:
It is immutable and unique for the same name -> Option A
Quick Check:
Symbol = immutable and unique [OK]
Hint: Symbols never change and are unique by name [OK]
Common Mistakes:
Thinking symbols can be modified like strings
Confusing symbols with regular strings
Assuming symbols store large data
Believing symbols allow spaces like strings
2.
Which of the following is the correct way to write a Ruby symbol?
?
easy
A. 'my_symbol'
B. "my_symbol"
C. my_symbol
D. :my_symbol
Solution
Step 1: Identify symbol syntax in Ruby
Symbols are written with a colon : followed by a name without quotes.
Step 2: Compare options
Only :my_symbol uses the correct symbol syntax; others are strings or invalid.
Final Answer:
:my_symbol -> Option D
Quick Check:
Symbol syntax = colon + name [OK]
Hint: Symbols start with a colon : followed by name [OK]
Common Mistakes:
Using quotes around symbols
Writing symbols without colon
Confusing symbols with strings
Using invalid characters in symbol names
3.
What will be the output of the following Ruby code?