From: Josh Cheek Date: 2010-04-15T12:24:20+09:00 Subject: Re: Is there a way to make class and object constants in Ruby? --000e0cd112d64716e904843dfede Content-Type: text/plain; charset=ISO-8859-1 On Wed, Apr 14, 2010 at 8:40 PM, Thomas Volkmar Worm 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. --000e0cd112d64716e904843dfede--