Challenge - 5 Problems
Symbol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code using symbols?
Consider the following Ruby code snippet. What will it print?
Ruby
sym1 = :hello
sym2 = :hello
puts sym1.object_id == sym2.object_idAttempts:
2 left
💡 Hint
Symbols with the same name share the same object in memory.
✗ Incorrect
In Ruby, symbols are immutable and unique. Two symbols with the same name point to the same object, so their object_ids are equal.
❓ Predict Output
intermediate2:00remaining
What happens when you try to modify a symbol?
What error will this Ruby code produce?
Ruby
sym = :example sym[0] = 'E'
Attempts:
2 left
💡 Hint
Symbols do not support string-like modification methods.
✗ Incorrect
Symbols are immutable and do not have methods like []= to modify them. Trying to call []= on a symbol raises NoMethodError.
🧠 Conceptual
advanced2:00remaining
Why are symbols considered immutable in Ruby?
Which of the following best explains why symbols are immutable?
Attempts:
2 left
💡 Hint
Think about what would happen if you changed a symbol's value.
✗ Incorrect
Symbols are immutable because they are unique and shared across the program. Changing one symbol would change all references, which is not allowed.
❓ Predict Output
advanced2:00remaining
What is the output of this code comparing symbols and strings?
What will this Ruby code print?
Ruby
puts :ruby == 'ruby' puts :ruby.eql?('ruby')
Attempts:
2 left
💡 Hint
Symbols and strings are different types even if they look similar.
✗ Incorrect
Symbols and strings are different objects and types in Ruby. Comparing them with == or eql? returns false.
❓ Predict Output
expert2:00remaining
How many unique objects are created in this Ruby code?
How many unique objects are created after running this code?
Ruby
arr = [:a, :b, :a, :c, :b] unique = arr.uniq puts unique.size
Attempts:
2 left
💡 Hint
uniq removes duplicates from the array.
✗ Incorrect
The array has symbols :a, :b, :a, :c, :b. The unique symbols are :a, :b, and :c, so size is 3.