rescue a Attempting-to-modify exception : freeze « String « Ruby






rescue a Attempting-to-modify exception

str = "This is a test. "
str.freeze

begin
  str << " Don't be alarmed."   # Attempting to modify
rescue => err
  puts "#{err.class} #{err}"
end

# Output:
#   TypeError: can't modify frozen string

 








Related examples in the same category

1.Make a string immutable with Object's freeze method
2.Freeze a string
3.is the object frozen?
4.can't modify frozen string