From: Xeno Campanoli on 19 Apr 2010 19:37 Thank you Josh and Thomas. The source of this question is my study of Erlang, where all 'Variables' do not vary after the initial assignment. I realized when reading up on the language that there are many areas in my classes where the thing I use, especially with Object Variables, are effectively constants, so I would do best to have them as actual constants. It turns out there is a lot of value in knowing that something cannot change, even if it can change but will have at least an obvious error. I don't expect to Erlangize my programs, but I have already identified things I can do to make my stuff safer and clearer just by making class and object variables into constants. Too bad there is not a special prefix for these as there is for the variables. That would be nice for referencing things. Still, no big deal. xc Josh Cheek wrote: > On Wed, Apr 14, 2010 at 8:40 PM, Thomas Volkmar Worm <tvw(a)s4r.de> wrote: > >> Am Wed, 14 Apr 2010 20:00:54 -0500 schrieb Xeno Campanoli: >> >>> I am reading up in other areas, and it occurs to me much of the items I >>> have as class and object variables really should be constants, but >>> ideally still in the class or object context. Is there a way to do >>> this? >>> >>> xc >> In class context you could do: >> >> class MyClass >> >> MYCONSTANT=6 >> >> def initialize >> puts MYCONSTANT >> end >> >> end >> >> >> But I wonder, whether it makes sense at all to have constants at object >> level. This sounds weird to me. >> >> Thomas >> >> > I do for things that don't (or shouldn't) change. For example, I'm working > on a Rails project where the admin can assign a level of trust to a user > such that they can post images directly to the main page from Twitter. The > database doesn't inherently support enumerated types, so the attribute is an > integer, then I have class level constants that map those values to their > meanings, and a set of methods that allow you to get booleans back when > asking if that user is able to do that authorized thing, and they are > embedded into named scopes so that I can pull all users who have this > authorization level, or that authorization level, and so forth. So I just > store a single integer in the database, but my application interprets it the > same across every instance. > -- "It's the preponderance, stupid!" - Professor Stephen Schneider, IPCC member |