Make a string immutable with Object's freeze method : freeze « String « Ruby






Make a string immutable with Object's freeze method


greet = "Hello, myValue!"
greet.freeze

# try to append something
greet.concat("!") # => TypeError: can't modify frozen string

# is the object frozen?
greet.frozen? # => true

 








Related examples in the same category

1.Freeze a string
2.is the object frozen?
3.can't modify frozen string
4.rescue a Attempting-to-modify exception