# Constant Hash strangeness

**URL:** <https://www.ruby-forum.com/t/constant-hash-strangeness/62141>\
**Category:** Ruby\
**Created:** [May 31, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/constant-hash-strangeness/62141 "2006-05-31T21:32:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wiktor\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@Wiktor\_M](https://www.ruby-forum.com/u/Wiktor_M)\
**Post date:** [May 31, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/constant-hash-strangeness/62141/1 "2006-05-31T21:32:09Z")

</div>

Hello.

This seems to be trivial, yet I can’t find an explanation for it  
anywhere online. Apologies if I’m asking the obvious.

If you create a constant hash:

```
 CONSTANTHASH = { :foo => "foobar" }
 x = CONSTANTHASH[:foo]
 x << "-suffix"
 p CONSTANTHASH

```

One gets the following output:

```
 { :foo => "foobar-suffix" }
 nil

```

So, this sort of makes sense if one considers that variables in Ruby  
are references. So Ruby only protects the hash, not the values keys in  
the hash point to. But… is there any way to protect the values, and  
not just the keys?

Thanks.

Warm Regards,

Wiktor M.

---

<div class="post-metadata">

**Author:** ![Wiktor\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@Wiktor\_M](https://www.ruby-forum.com/u/Wiktor_M)\
**Post date:** [May 31, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/constant-hash-strangeness/62141/2 "2006-05-31T21:48:43Z")

</div>

On Thu, 1 Jun 2006 04:29:39 +0900, “Wiktor M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> p CONSTANTHASH
> 
> ```

It’s not a constant hash, but rather a constant which holds a reference  
to a hash. The constant can’t be changed to point to a different hash  
(well … not without a warning anyway), but the hash itself can still  
be modified:

CONSTANTHASH = { :foo =\> “foobar” }  
CONSTANTHASH[:bar] = “zoom”  
p CONSTANTHASH[:bar]

If you want to render an object immune to modification, freeze it.

CONSTANTHASH = { :foo =\> “foobar”.freeze }.freeze

(It isn’t necessary to freeze “value types” like symbols or fixnums,  
though, since – unlike strings or hashes – they are naturally  
immutable.)

-mental

---

<div class="post-metadata">

**Author:** ![Wiktor\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@Wiktor\_M](https://www.ruby-forum.com/u/Wiktor_M)\
**Post date:** [May 31, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/constant-hash-strangeness/62141/3 "2006-05-31T21:48:54Z")

</div>

On 5/31/06, Wiktor M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> p CONSTANTHASH
> 
> ```
> 
> One gets the following output:
> 
> ```
> { :foo => "foobar-suffix" }
> nil
> 
> ```
> 
> So, this sort of makes sense if one considers that variables in Ruby  
> are references. So Ruby only protects the hash, not the values keys in  
> the hash point to. But… is there any way to protect the values, and  
> not just the keys?

Constants don’t work the way that you think they do. I’ll be posting  
an article about this soon; I just haven’t decided where to put it.

-austin

---

<div class="post-metadata">

**Author:** ![Wiktor\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@Wiktor\_M](https://www.ruby-forum.com/u/Wiktor_M)\
**Post date:** [June 1, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/constant-hash-strangeness/62141/4 "2006-06-01T15:21:54Z")

</div>

Hello.

Well, thanks for taking the time to do the article… and if you need  
anyone to proof-read/test it. 🙂

Cheers.

Wiktor

---

<div class="post-metadata">

**Author:** ![Wiktor\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@Wiktor\_M](https://www.ruby-forum.com/u/Wiktor_M)\
**Post date:** [June 1, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/constant-hash-strangeness/62141/5 "2006-06-01T15:22:04Z")

</div>

Thank you. I was surprised by the way hash behaves, but I do  
understand why. Its obvious in hind-sight, I guess you could say. But  
thanks for the .freeze example, that’s exactly what I was look for.

Many thanks,

Wiktor
