# String freeze

**URL:** <https://www.ruby-forum.com/t/string-freeze/92772>\
**Category:** Ruby\
**Created:** [April 24, 2007, 2:10am UTC](https://www.ruby-forum.com/t/string-freeze/92772 "2007-04-24T02:10:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [April 24, 2007, 2:10am UTC](https://www.ruby-forum.com/t/string-freeze/92772/1 "2007-04-24T02:10:13Z")

</div>

Hey all,

So I’m trying to freeze a class variable, and I discover that  
Object#freeze  
breaks down every time on my machine. Even with simple strings.

irb(main):001:0\> test = “good”  
=\> “good”  
irb(main):002:0\> test.freeze  
=\> “good”  
irb(main):003:0\> test.frozen?  
=\> true  
irb(main):004:0\> test = “bad”  
=\> “bad”  
irb(main):005:0\> test.frozen?  
=\> false

What gives?

I must be missing something.

fiery

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [April 24, 2007, 2:25am UTC](https://www.ruby-forum.com/t/string-freeze/92772/2 "2007-04-24T02:25:14Z")

</div>

On 4/23/07, Stephen S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> true  
> irb(main):004:0\> test = “bad”  
> =\> “bad”  
> irb(main):005:0\> test.frozen?  
> =\> false
> 
> What gives?
> 
> I must be missing something.

You are missing the difference between a variable and an object.

test = “good”

test is a local variable which references a string object.

test.freeze?

This freezes the object referenced by test.

test = “bad”  
this makes the variable test refer to an entirely different string.

For another thing to ponder try this:

test = “good”  
test.freeze

another\_variable = test  
another\_variable.frozen? =\> true

The point is that you can’t freeze variables.

This distinction between variables and the objects they reference is  
a stumbling block for many people coming to Ruby if they haven’t been  
exposed to other languages in the family.

–  
Rick DeNatale

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

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [April 24, 2007, 3:06am UTC](https://www.ruby-forum.com/t/string-freeze/92772/3 "2007-04-24T03:06:17Z")

</div>

On 4/24/07, Stephen S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey all,
> 
> So I’m trying to freeze a class variable, and I discover that Object#freeze  
> breaks down every time on my machine. Even with simple strings.
> 
> fiery

Try altering it instead of reassigning it.  
I think this is a little more like what you expected.  
Not exactly, but you can see freezing.

test = “good”  
p test  
test.freeze  
p test  
p test.frozen?

test \<\< “bad”  
p test  
p test.frozen?

Harry

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [April 24, 2007, 5:49am UTC](https://www.ruby-forum.com/t/string-freeze/92772/4 "2007-04-24T05:49:26Z")

</div>

test references two different objects… try this…

test = “good”  
test.freeze  
test.object\_id

test = “bad”  
test.object\_id

You will notice different object\_id’s in the first and later case.

-abhijit

On 4/24/07, Harry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Try altering it instead of reassigning it.  
> test \<\< “bad”  
> p test  
> p test.frozen?
> 
> Harry
> 
> –  
> [Kakueki.com is for sale | HugeDomains](http://www.kakueki.com/ruby/list.html)  
> A Look into Japanese Ruby List in English

–

à¤…à¤­à¤¿à¤œà¥€à¤¤

[written in [http://www.paahijen.com/scratchpad](http://www.paahijen.com/scratchpad)]

[[http://www.paahijen.com](http://www.paahijen.com)]
