0
0
Rubyprogramming~10 mins

String freezing for 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 freeze the string so it cannot be changed.

Ruby
name = "Ruby".[1]
Drag options to blanks, or click blank then click option'
Aupcase
Bfreeze
Creverse
Dcapitalize
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods like upcase or reverse which change the string but do not freeze it.
2fill in blank
medium

Complete the code to check if the string is frozen.

Ruby
str = "hello".freeze
puts str.[1]
Drag options to blanks, or click blank then click option'
Afreeze?
Bfrozen
Cfrozen?
Dis_frozen
Attempts:
3 left
💡 Hint
Common Mistakes
Using freeze instead of frozen? to check.
3fill in blank
hard

Complete the code to append '!' to the greeting.

Ruby
greeting = "Hi"
greeting.[1]("!")
Drag options to blanks, or click blank then click option'
Aconcat
Bupcase!
Cfreeze # Then try to modify
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to modify a frozen string causes error.
Using freeze before modification causes error.
4fill in blank
hard

Fill both blanks to create a frozen string and check if it is frozen.

Ruby
word = "immutable".[1]
puts word.[2]
Drag options to blanks, or click blank then click option'
Afreeze
Bupcase
Cfrozen?
Dreverse
Attempts:
3 left
💡 Hint
Common Mistakes
Checking frozen? before freezing the string.
Using methods like upcase or reverse instead of freeze.
5fill in blank
hard

Fill all three blanks to create a frozen string, check if frozen, and try to modify it safely.

Ruby
text = "safe".[1]
if text.[2]
  puts "Cannot modify frozen string"
else
  text.[3]("!")
end
Drag options to blanks, or click blank then click option'
Afreeze
Bfrozen?
Cconcat
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to modify a frozen string without checking.
Using wrong methods to modify or check.