Congelación: freezing

>> person2 = person1
=> "Wim"
>> person1.freeze
=> "Wim"
>> person2[0] = "R"
TypeError: can't modify frozen string
  from (irb):18:in `[]='
  from (irb):18

~/rubytesting$ cat -n freeze.rb 
     1  begin
     2    a = [1,2,3].freeze
     3    puts a
     4    a << [4,7] # this raises an exception
     5    puts a
     6  rescue
     7    puts "Exception <#{$!.class}> raised <#{$!}>"
     8  end
~/rubytesting$ ruby freeze.rb 
1
2
3
Exception <TypeError> raised <can't modify frozen array>

pp@nereida:~/LPPbook$ irb
irb(main):001:0> 
irb(main):002:0* 
irb(main):003:0* quit
lpp@nereida:~/LPPbook$ irb
irb(main):001:0> q = "hello"
=> "hello"
irb(main):002:0> q.freeze
=> "hello"
irb(main):003:0> q.object_id
=> 70233414821660
irb(main):004:0> q += " world"
=> "hello world"
irb(main):005:0> q.object_id
=> 70233414771300
irb(main):006:0> q.freeze
=> "hello world"
irb(main):007:0> q << " world"
TypeError: can't modify frozen string
        from (irb):7:in `<<'
        from (irb):7
        from :0

No hay manera de descongelar un objeto sobre el que ha sido llamado freeze.



Casiano Rodriguez León 2015-06-18