Bird
Raised Fist0
Rubyprogramming~15 mins

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

Choose your learning style10 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
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

  1. 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.
  2. Step 2: Recognize uniqueness of Symbols

    Two Symbols with the same name always refer to the same object, making them unique and memory efficient.
  3. Final Answer:

    It is immutable and unique for the same name -> Option A
  4. 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

  1. Step 1: Identify symbol syntax in Ruby

    Symbols are written with a colon : followed by a name without quotes.
  2. Step 2: Compare options

    Only :my_symbol uses the correct symbol syntax; others are strings or invalid.
  3. Final Answer:

    :my_symbol -> Option D
  4. 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?

sym1 = :apple
sym2 = :apple
puts sym1.object_id == sym2.object_id
medium
A. false
B. nil
C. true
D. SyntaxError

Solution

  1. Step 1: Understand symbol object identity

    Symbols with the same name share the same object_id because they are unique and immutable.
  2. Step 2: Compare object_ids of sym1 and sym2

    Both :apple symbols point to the same object, so their object_ids are equal, making the comparison true.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    Same symbol = same object_id [OK]
Hint: Same symbol names share object_id, so comparison is true [OK]
Common Mistakes:
  • Assuming different symbols have different object_ids
  • Confusing symbols with strings which differ
  • Expecting false because of separate variables
  • Thinking symbols are mutable objects
4.

Find the error in this Ruby code snippet:

sym = :hello
sym << " world"
puts sym
medium
A. Symbols can be changed with << operator
B. Symbols cannot be concatenated with <<
C. Syntax error in symbol declaration
D. Missing quotes around symbol

Solution

  1. Step 1: Check symbol mutability

    Symbols are immutable, so you cannot change them or add to them with operators like <<.
  2. Step 2: Identify the error in code

    The line sym << " world" tries to modify the symbol, which causes a runtime error.
  3. Final Answer:

    Symbols cannot be concatenated with << -> Option B
  4. Quick Check:

    Symbols are immutable, no << allowed [OK]
Hint: Symbols cannot be changed or concatenated [OK]
Common Mistakes:
  • Trying to modify symbols like strings
  • Assuming symbols can be concatenated
  • Confusing syntax errors with runtime errors
  • Using << operator on immutable objects
5.

Given the following code, what will be the output?

hash = { apple: 1, banana: 2 }
key = :apple
hash[key] = 3
puts hash[:apple]

Consider the immutability and identity of symbols in your answer.

hard
A. 3
B. 1
C. nil
D. KeyError

Solution

  1. Step 1: Understand symbol keys in hash

    Symbols used as keys are immutable and unique, so :apple and key (which is :apple) refer to the same key.
  2. Step 2: Update hash value for :apple

    Assigning hash[key] = 3 updates the value for the :apple key from 1 to 3.
  3. Step 3: Output the updated value

    puts hash[:apple] prints the updated value 3.
  4. Final Answer:

    3 -> Option A
  5. Quick Check:

    Symbol keys are unique, so value updates correctly [OK]
Hint: Symbol keys are unique; updating key changes value [OK]
Common Mistakes:
  • Thinking key is different because of variable
  • Expecting original value 1 instead of updated 3
  • Confusing symbols with strings as keys
  • Assuming symbols can be duplicated in hash keys
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard