Complete the code to freeze the string so it cannot be changed.
name = "Ruby".[1]
Using freeze on a string makes it immutable, so it cannot be changed later.
Complete the code to check if the string is frozen.
str = "hello".freeze puts str.[1]
freeze instead of frozen? to check.The method frozen? returns true if the string is frozen (immutable).
Complete the code to append '!' to the greeting.
greeting = "Hi" greeting.[1]("!")
freeze before modification causes error.concat("!") appends "!" to the string in place. This would raise a FrozenError if the string was frozen.
Fill both blanks to create a frozen string and check if it is frozen.
word = "immutable".[1] puts word.[2]
First, freeze the string to make it immutable. Then check if it is frozen using frozen?.
Fill all three blanks to create a frozen string, check if frozen, and try to modify it safely.
text = "safe".[1] if text.[2] puts "Cannot modify frozen string" else text.[3]("!") end
Freeze the string first. Check if it is frozen with frozen?. If not frozen, modify it with concat. This avoids errors.