0
0
Rubyprogramming~10 mins

Symbol type and immutability in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a symbol named :apple.

Ruby
fruit = [1]
Drag options to blanks, or click blank then click option'
A'apple'
B:apple
Capple
D"apple"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes creates a string, not a symbol.
Leaving out the colon creates a variable or method name.
2fill in blank
medium

Complete the code to check if two symbols are the same object.

Ruby
puts :cat.[1] :cat
Drag options to blanks, or click blank then click option'
A!=
B==
Ceql?
Dequal?
Attempts:
3 left
💡 Hint
Common Mistakes
Using == checks value equality but not object identity.
Using != is for inequality.
3fill in blank
hard

Fix the error in the code to convert a string to a symbol.

Ruby
sym = "dog".[1]
Drag options to blanks, or click blank then click option'
Ato_sym!
Bto_s
Cto_sym
Dto_str
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_s returns a string, not a symbol.
There is no to_sym! method.
4fill in blank
hard

Fill both blanks to create a hash with symbol keys and access a value.

Ruby
person = { [1] => "Alice", [2] => 30 }
age = person[:age]
Drag options to blanks, or click blank then click option'
A:name
B:age
C"age"
D"name"
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings as keys when symbols are expected.
Mixing symbol and string keys inconsistently.
5fill in blank
hard

Fill all three blanks to demonstrate symbol immutability and comparison.

Ruby
sym1 = :car
sym2 = :car
puts sym1 [1] sym2
puts sym1.object_id [2] sym2.object_id
# Trying to change symbol (will cause error)
sym3 = :bike
# sym3 [3] "cycle" # Uncommenting causes error
Drag options to blanks, or click blank then click option'
A==
B!=
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality operators when equality is expected.
Trying to modify a symbol with methods like replace which do not exist.