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