Olivier
3/15/2007 3:47:00 PM
Le jeudi 15 mars 2007 16:35, jwaala.amplify@gmail.com a écrit :
> >> a='asdf'
>
> => "asdf"
>
> >> a.freeze
>
> => "asdf"
>
> >> a='qwer'
>
> => "qwer"
>
> should'nt that throw and exception ???
No, because calling the freeze method prevents from modifying the object, it
doesn't prevent from modifying the variable. Remember that a variable is just
a way to give a name to an object, but it is not tied to this object forever.
So, your code doesn't raise an exception, but this one does :
irb(main):001:0> a="abc"
=> "abc"
irb(main):002:0> a.freeze
=> "abc"
irb(main):003:0> a << 'd'
TypeError: can't modify frozen string
from (irb):3:in `<<'
from (irb):3
What you are looking for is closer of a constant, actually :
irb(main):004:0> A="abc"
=> "abc"
irb(main):005:0> A="def"
(irb):5: warning: already initialized constant A
--
Olivier Renaud