On Thu, 1 Jun 2006 04:29:39 +0900, "Wiktor Macura" <wmacura / gmail.com> wrote:
> 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

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