From: Eric Hodel Date: 2005-10-21T05:05:48+09:00 Subject: Re: DbC, const and freeze. On Oct 20, 2005, at 7:01 AM, Ryan Leavengood wrote: > On 10/19/05, John Carter wrote: > >> >> For example, look at this hole... >> irb >> irb(main):001:0> a = [[1,2],[3,4]] >> => [[1, 2], [3, 4]] >> irb(main):002:0> a.freeze >> => [[1, 2], [3, 4]] >> irb(main):003:0> a << [9] >> RuntimeError: can't modify frozen array >> from (irb):3:in `<<' >> from (irb):3 >> from :0 >> irb(main):004:0> a[0]<<1 >> => [1, 2, 1] >> irb(main):005:0> a >> => [[1, 2, 1], [3, 4]] >> irb(main):006:0> >> >> Even though "a" was frozen, I still could modify the things "a" >> contained. >> > > Fair enough. How about this: > > class Object > def deep_freeze > self.freeze > if self.respond_to? :each > self.each do |i| > i.deep_freeze > end > end > end > end Does not freeze instance variables. instance_variables.each do |ivar| instance_variable_get(ivar).deep_freeze end I think. -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04