# Freeze method doesn't work

**URL:** <https://www.ruby-forum.com/t/freeze-method-doesnt-work/197452>\
**Category:** Ruby\
**Created:** [October 14, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/freeze-method-doesnt-work/197452 "2010-10-14T17:00:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 14, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/freeze-method-doesnt-work/197452/1 "2010-10-14T17:00:51Z")

</div>

Freeze should prevent further modifications to ‘var’…

mac-undrgrnd:~ yspro$ irb  
ruby-1.9.2-p0 \> var = 4  
=\> 4  
ruby-1.9.2-p0 \> var.frozen?  
=\> false  
ruby-1.9.2-p0 \> var.freeze  
=\> 4  
ruby-1.9.2-p0 \> var.frozen?  
=\> true  
ruby-1.9.2-p0 \> var = 5  
=\> 5  
ruby-1.9.2-p0 \> var.frozen?  
=\> false  
ruby-1.9.2-p0 \>

What’s wrong?

---

<div class="post-metadata">

**Author:** ![sergey\_u](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sergey\_u](https://www.ruby-forum.com/u/sergey_u)\
**Post date:** [October 14, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/freeze-method-doesnt-work/197452/2 "2010-10-14T17:23:29Z")

</div>

Thank you Jeremy a lot!

---

<div class="post-metadata">

**Author:** ![sergey\_u](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sergey\_u](https://www.ruby-forum.com/u/sergey_u)\
**Post date:** [October 14, 2010, 5:21pm UTC](https://www.ruby-forum.com/t/freeze-method-doesnt-work/197452/3 "2010-10-14T17:21:42Z")

</div>

On 10/14/2010 10:00 AM, Sergey U. wrote:

> =\> true  
> ruby-1.9.2-p0 \> var = 5  
> =\> 5  
> ruby-1.9.2-p0 \> var.frozen?  
> =\> false  
> ruby-1.9.2-p0 \>
> 
> What’s wrong?

The freeze method doesn’t operate on var itself. It operates on the  
object that var references, so the Fixnum object for 4 was frozen in  
your test. However, you are allowed to replace that object with another  
any time you please. The freeze method only prevents you from changing  
the object itself:

irb(main):001:0\> var = ‘abc123’  
=\> “abc123”  
irb(main):002:0\> var.frozen?  
=\> false  
irb(main):003:0\> var.freeze  
=\> “abc123”  
irb(main):004:0\> var.frozen?  
=\> true  
irb(main):005:0\> var.sub!(/\d/, ‘n’)  
TypeError: can’t modify frozen string  
from (irb):5:in `sub!’  
from (irb):5  
from :0  
irb(main):006:0\> var  
=\> “abc123”  
irb(main):007:0\> var = ‘abcn23’  
=\> “abcn23”  
irb(main):008:0\> var.frozen?  
=\> false

-Jeremy

---

<div class="post-metadata">

**Author:** ![sergey\_u](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sergey\_u](https://www.ruby-forum.com/u/sergey_u)\
**Post date:** [October 15, 2010, 10:59pm UTC](https://www.ruby-forum.com/t/freeze-method-doesnt-work/197452/4 "2010-10-15T22:59:09Z")

</div>

On Thu, Oct 14, 2010 at 5:00 PM, Sergey U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> true  
> ruby-1.9.2-p0 \> var = 5  
> =\> 5  
> ruby-1.9.2-p0 \> var.frozen?  
> =\> false  
> ruby-1.9.2-p0 \>
> 
> What’s wrong?

Variables reference objects. It’s the object you can freeze, not the  
variable. If you make the variable point to a different object, the  
original one is still frozen, but the new one maybe not.

Jesus.

---

<div class="post-metadata">

**Author:** ![sergey\_u](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sergey\_u](https://www.ruby-forum.com/u/sergey_u)\
**Post date:** [October 14, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/freeze-method-doesnt-work/197452/5 "2010-10-14T17:24:12Z")

</div>

On Thursday 14 October 2010, Sergey U. wrote:

> |Freeze should prevent further modifications to ‘var’…

No. freeze prevents modifications to the object contained in var. For  
example:

str = “hello”  
str.upcase!  
puts str  
=\> HELLO  
str.freeze  
str.downcase!  
=\> RuntimeError: can’t modify frozen string

str1 = str  
str1.downcase!  
=\> RuntimeError: can’t modify frozen string

Note that freeze is useless on classes (like Integer or Symbol) which  
don’t  
have destructive methods.

The nearest you can come to prevent modifications to a variable is to  
use a  
constant instead (but remember that ruby allows you to assign a  
different  
value to a constant, it just emits a warning). However, the object the  
constant contains can be freely modified:

CONST=“hello”  
CONST.upcase!  
puts CONST  
“HELLO”  
CONST=“bye”  
=\> warning: already initialized constant CONST  
puts CONST  
=\> “bye”

If you also want to prevent modifications to the object in the constant,  
freeze it.

I hope this helps

Stefano
