0
0
Rubyprogramming~15 mins

Symbol type and immutability in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Symbol type and immutability
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use puts to display the hash on the screen.