Forum: Ruby Constant Hash strangeness

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1dae99aa90f8b641a45f39c17c4f0fd6?d=identicon&s=25 Wiktor Macura (Guest)
on 2006-05-31 21:32
(Received via mailing list)
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 Macura
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 MenTaLguY (Guest)
on 2006-05-31 21:48
(Received via mailing list)
On Thu, 1 Jun 2006 04:29:39 +0900, "Wiktor Macura" <wmacura@gmail.com>
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
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-05-31 21:48
(Received via mailing list)
On 5/31/06, Wiktor Macura <wmacura@gmail.com> 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
1dae99aa90f8b641a45f39c17c4f0fd6?d=identicon&s=25 Wiktor Macura (Guest)
on 2006-06-01 15:21
(Received via mailing list)
Hello.

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

Cheers.

Wiktor
1dae99aa90f8b641a45f39c17c4f0fd6?d=identicon&s=25 Wiktor Macura (Guest)
on 2006-06-01 15:22
(Received via mailing list)
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
This topic is locked and can not be replied to.