0
0
Rubyprogramming~10 mins

Symbol type and immutability in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Symbol type and immutability
Create Symbol :name
Symbol stored in memory
Use Symbol in code
Symbol is immutable
Reuse same Symbol object
No new object created
Symbols are unique, immutable names stored once in memory and reused whenever referenced.
Execution Sample
Ruby
:name.object_id == :name.object_id
:name.to_s
name = :name
name = :other
This code shows that the same symbol has the same object id, symbols are immutable, and assigning symbols.
Execution Table
StepCodeActionResult/Output
1:name.object_idGet object id of :name symbole.g. 123456 (fixed id)
2:name.object_idGet object id of :name symbol againSame as step 1, e.g. 123456
3:name.object_id == :name.object_idCompare object idstrue (same object)
4:name.to_sConvert symbol to string"name"
5name = :nameAssign symbol :name to variable nameVariable name points to symbol :name
6name = :otherAssign symbol :other to variable nameVariable name points to symbol :other
💡 Symbols are immutable and reused, so object ids for the same symbol are always equal.
Variable Tracker
VariableStartAfter Step 5After Step 6
nameundefined:name (symbol):other (symbol)
Key Moments - 3 Insights
Why does :name.object_id return the same value every time?
Because symbols are stored once in memory and reused, so the object id is always the same for the same symbol (see steps 1, 2, and 3).
Can we change the content of a symbol like a string?
No, symbols are immutable, so you cannot change their content. Converting to string (:name.to_s) creates a new string but does not change the symbol (see step 4).
Does assigning a new symbol to a variable create a new symbol object?
No, symbols are created once and reused. Assigning a new symbol just points the variable to a different existing symbol (see steps 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of comparing :name.object_id == :name.object_id at step 3?
Atrue
Bfalse
Cnil
Derror
💡 Hint
Check step 3 in the execution_table where the comparison is made.
At which step does the variable name change to point to a different symbol?
AStep 4
BStep 5
CStep 6
DStep 3
💡 Hint
Look at variable_tracker and execution_table steps 5 and 6.
If symbols were mutable, what would change in the execution table?
AThe object ids would differ for the same symbol
BThe comparison at step 3 would be false
CThe to_s method would change the symbol itself
DAssigning a new symbol would create a new object id
💡 Hint
Refer to key_moments about immutability and step 3 comparison.
Concept Snapshot
Symbols are unique, immutable names stored once in memory.
They have a fixed object id reused everywhere.
Symbols cannot be changed like strings.
Assigning symbols just points variables to existing symbols.
Use symbols for identifiers needing fast, memory-efficient reuse.
Full Transcript
Symbols in Ruby are special names that are stored only once in memory. When you write :name multiple times, Ruby uses the same symbol object each time. This means the object id of :name is always the same, as shown in steps 1, 2, and 3. Symbols are immutable, so you cannot change their content like strings. For example, :name.to_s returns a string "name" but does not change the symbol itself (step 4). When you assign a symbol to a variable, like name = :name or name = :other, you are just pointing the variable to an existing symbol object (steps 5 and 6). This makes symbols very efficient for identifiers that do not need to change. Understanding that symbols are immutable and reused helps avoid confusion about object ids and variable assignments.