How to unfreeze an object in Ruby?

后端 未结 4 1238
生来不讨喜
生来不讨喜 2020-12-13 17:50

In Ruby, there is Object#freeze, which prevents further modifications to the object:

class Kingdom
  attr_accessor :weather_conditions
end

arendelle = Kingd         


        
4条回答
  •  -上瘾入骨i
    2020-12-13 18:20

    frozen_object = %w[hello world].freeze
    frozen_object.concat(['and universe']) # FrozenError (can't modify frozen Array)
    frozen_object.dup.concat(['and universe']) # ['hello', 'world', 'and universe']
    

提交回复
热议问题