# Freeze not freezing

**URL:** <https://www.ruby-forum.com/t/freeze-not-freezing/178500>\
**Category:** Ruby\
**Created:** [November 5, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/freeze-not-freezing/178500 "2009-11-05T15:06:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 5, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/freeze-not-freezing/178500/1 "2009-11-05T15:06:04Z")

</div>

Note that I am new to this list and to Ruby.

Consider …

irb(main):020:0\> c=[0,1]  
=\> [0, 1]  
irb(main):021:0\> c[0].freeze  
=\> 0  
irb(main):022:0\> c[0]=2  
=\> 2  
irb(main):023:0\> c  
=\> [2, 1]  
irb(main):024:0\>

Similarly …

irb(main):029:0\> f=1  
=\> 1  
irb(main):030:0\> f.freeze  
=\> 1  
irb(main):031:0\> f=2  
=\> 2

It took a few moments for me to scratch my head and then I thought  
that the reason that there was no message when c[0] or f changed was  
that  
c[0] is not an object but a reference to an object (the number 2).

So I searched the archives and it appears taht my reasoning is  
correct. I think.

there was the suggestion to make f a constant, F. Yes, that generates  
a warning … but it is not what I want. I want to freeze the  
reference as well as the object.

In C++ it is possible to “freeze” (i.e. const) both a pointer to an  
object and the object itself. Is this possible in Ruby?

a) Is my reasoning correct?

b) Is there a way to do what I want? That is, freeze f or c[0] so that  
I get some sort of message/error/exception?

c) Do my examples violate POLS?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 5, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/freeze-not-freezing/178500/2 "2009-11-05T15:17:12Z")

</div>

Hi,

In message “Re: Freeze not freezing”  
on Thu, 5 Nov 2009 23:05:28 +0900, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|a) Is my reasoning correct?

#freeze protect an object from modification. The reference is not the  
target of freezing.

|b) Is there a way to do what I want? That is, freeze f or c[0] so that  
|I get some sort of message/error/exception?

You can protect an array (or object) as a whole, since it is an  
object, so that

c=[0,1]  
c.freeze  
c[0]=2 # =\> can’t modify frozen array (RuntimeError)

but you cannot freeze any variables.

|c) Do my examples violate POLS?

No. Unlike C++, variables and references are not object in any sense  
in Ruby. From my viewpoint, C++ violates POLS. 😉

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 5, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/freeze-not-freezing/178500/3 "2009-11-05T18:40:55Z")

</div>

On 2009-11-05, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> From my viewpoint, C++ violates POLS. 😉

Sturgeon’s Law: 90% of everything is crud.

If you know this, C++ does not violate POLS. 🙂

-s

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 5, 2009, 5:11pm UTC](https://www.ruby-forum.com/t/freeze-not-freezing/178500/4 "2009-11-05T17:11:20Z")

</div>

Matz,

I deeply appreciate your answer.

But I will repeat part of the question: Is there a way to detect …  
in the general case … that the reference to an object has changed?

In a more C-like environment, a debugger is able to detect (via  
hardware) that a location in memory has changed. Is something similar  
available in Ruby?

Where is the Ruby debugger (watch, etc.) documented?

Ralph

Thursday, November 5, 2009, 7:14:11 AM, you wrote:

YM\> Hi,

YM\> In message “Re: Freeze not freezing”  
YM\> on Thu, 5 Nov 2009 23:05:28 +0900, Ralph S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

YM\> |a) Is my reasoning correct?

YM\> #freeze protect an object from modification. The reference is not  
the  
YM\> target of freezing.

YM\> |b) Is there a way to do what I want? That is, freeze f or c[0] so  
that  
YM\> |I get some sort of message/error/exception?

YM\> You can protect an array (or object) as a whole, since it is an  
YM\> object, so that

YM\> c=[0,1]  
YM\> c.freeze  
YM\> c[0]=2 # =\> can’t modify frozen array (RuntimeError)

YM\> but you cannot freeze any variables.

YM\> |c) Do my examples violate POLS?

YM\> No. Unlike C++, variables and references are not object in any  
sense  
YM\> in Ruby. From my viewpoint, C++ violates POLS. 😉

YM\> matz.
