0
0
Rubyprogramming~10 mins

Why strings are mutable in Ruby - Test Your Understanding

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

Complete the code to change the string content.

Ruby
str = "hello"
str[1] " world"
puts str
Drag options to blanks, or click blank then click option'
A<<
B+=
C==
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using + which creates a new string instead of modifying.
2fill in blank
medium

Complete the code to replace part of the string.

Ruby
str = "hello"
str[1] 0, 5, "hi"
puts str
Drag options to blanks, or click blank then click option'
A[]=
Breplace
Cslice
Dsub
Attempts:
3 left
💡 Hint
Common Mistakes
Using sub which returns a new string without modifying original.
3fill in blank
hard

Fix the error in the code to mutate the string.

Ruby
str = "hello"
str.[1](" world")
puts str
Drag options to blanks, or click blank then click option'
Asub
Bconcat
Cgsub
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using sub or gsub which return new strings without mutating.
4fill in blank
hard

Fill both blanks to create a mutable string and modify it.

Ruby
str = [1]"hello"
str[2] " world"
puts str
Drag options to blanks, or click blank then click option'
A"hello"
BString.new(
C<<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using + which creates a new string instead of modifying.
5fill in blank
hard

Fill all three blanks to replace part of the string and show mutability.

Ruby
str = "hello"
str[1] 0, 5, [2]
puts str
puts str[3] "!"
Drag options to blanks, or click blank then click option'
A[]=
B"hi"
C<<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using + which creates new strings instead of mutating.