Ruby/String/freeze
Материал из Wiki.crossplatform.ru
Версия от 17:56, 13 сентября 2010; ViGOur (Обсуждение | вклад)
Содержание |
can"t modify frozen string
greet = "Hello, myValue!" greet.freeze greet.concat("!") # => TypeError: can"t modify frozen string
Freeze a string
greet = "Hello, myValue!" greet.freeze
is the object frozen?
greet = "Hello, myValue!" greet.frozen? # => true
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
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