From: Robert Klemme Date: 2008-05-22T20:50:26+09:00 Subject: Re: Prevent ruby constant variables from changing? On 22.05.2008 13:01, Dave Bass wrote: > George Wang wrote: >> However, the constant variable can still be >> reassigned even though a warning is issued. > > Am I the only person who thinks that constants should be unchangeable? Probably not. However, there are more spots like this in Ruby (e.g. private methods). It is a general feature of the language to not be too restrictive about such things. For me this was never an issue. YMMV though. OTOH this makes some things (mostly related to meta programming) pretty easy that are hard to impossible in other languages. For example, if you want to write code in Java that modifies arbitrary private instance variables, you can do it but it's much more involved than using #instance_variable_set. It's probably good that it is not made too easy in Java - and equally good that it's the way it is in Ruby. > Being new to Ruby, it seems weird to me that constants aren't actually > constant. I'm sure there must be some arcane reason for it, but surely > most people want their constants to remain constant! That's how it works > in most languages. > > Freezing would seem like a good idea but I haven't actually tried it. > Yet. Freezing does something else: freezing prohibits assigning to and creation of instance variables. This helps if you want to use an instance of an otherwise mutable class as constant. I agree with Pena that the safest approach is probably the one he demonstrated. Kind regards robert