# Freeze string fails to freeze?

**URL:** <https://www.ruby-forum.com/t/freeze-string-fails-to-freeze/88917>\
**Category:** Ruby\
**Created:** [March 15, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/freeze-string-fails-to-freeze/88917 "2007-03-15T16:35:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 15, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/freeze-string-fails-to-freeze/88917/1 "2007-03-15T16:35:43Z")

</div>

> > a=‘asdf’  
> > =\> “asdf”
> 
> > a.freeze  
> > =\> “asdf”
> 
> > a=‘qwer’  
> > =\> “qwer”

should’nt that throw and exception ???

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 15, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/freeze-string-fails-to-freeze/88917/2 "2007-03-15T16:45:45Z")

</div>

On 15.03.2007 16:33, removed\_email\_address@domain.invalid wrote:

> > > a=‘asdf’  
> > > =\> “asdf”
> > 
> > > a.freeze  
> > > =\> “asdf”
> > 
> > > a=‘qwer’  
> > > =\> “qwer”
> 
> should’nt that throw and exception ???

No, the _object_ is frozen. This has no effects on the _variable_.

> > a=‘asdf’  
> > =\> “asdf”
> 
> > a.freeze  
> > =\> “asdf”
> 
> > a \<\< “foo”  
> > TypeError: can’t modify frozen string  
> > from (irb):4:in `\<\<’  
> > from (irb):4  
> > from :0

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 15, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/freeze-string-fails-to-freeze/88917/3 "2007-03-15T16:47:47Z")

</div>

Le jeudi 15 mars 2007 16:35, removed\_email\_address@domain.invalid a écrit :

> =\> “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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 15, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/freeze-string-fails-to-freeze/88917/4 "2007-03-15T16:49:47Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > > a=‘asdf’  
> > > =\> “asdf”  
> > > a.freeze  
> > > =\> “asdf”  
> > > a=‘qwer’  
> > > =\> “qwer”
> 
> should’nt that throw and exception ???

freeze is a method on the object referenced by a,  
not on the variable a (a is just a name, not the  
object itself).

a = ‘abcd’  
a.freeze

# a[2]=‘w’ raises

# ./freezetest.rb:7:in `[]=': can’t modify frozen string (TypeError)

This causes an exception because you are trying to modify the  
frozen object.

a = ‘defg’

This does not, because the object (which up to now was  
referenced by a) is not changed. a now refers to a  
different object.

Try it with a constant:

A = ‘abcd’  
A = ‘defg’ # raises a warning: already initialized constant A

–

Regards,

Fergal Byrne - Technical Director

Adnet: Web Builders to the Design Industry

[http://www.adnet.ie/](http://www.adnet.ie/) t:+353 1 855 8951 aim/skype:FergByrne

=== We’ve Moved! 63 Lower Gardiner Street, Dublin 1 ===

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 16, 2007, 12:30am UTC](https://www.ruby-forum.com/t/freeze-string-fails-to-freeze/88917/5 "2007-03-16T00:30:00Z")

</div>

Le jeudi 15 mars 2007 23:16, Rick DeNatale a écrit :

> On 3/15/07, Olivier R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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.
> 
> Is it just me or has the confusion between variables and objects been  
> coming up more frequently than usual of late?

Not more often than the elsif/elseif confusion 😃

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 15, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/freeze-string-fails-to-freeze/88917/6 "2007-03-15T23:16:50Z")

</div>

On 3/15/07, Olivier R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

Is it just me or has the confusion between variables and objects been  
coming up more frequently than usual of late?

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
