From: David Alan Black Date: 2002-04-01T08:22:34+09:00 Subject: Re: Detecting class variables.... Hi -- On Mon, 1 Apr 2002, Sean Chittenden wrote: > > > I have a class that will have many objects and each object has a > > > rather expensive regexp that I don't want to recompile every instance. > > > I was hoping to do something similar to: > > > > > > if @@httpd_combined_entry_re.nil? > > > @@httpd_combined_entry_re = Regexp.new('some regexp string') > > > end > > > > > > > > > Without using Modules#class_variables, there anyway to see if the > > > symbol for a class variable is populated? -sc > > > > Could you get by with the ||= assignment idiom? > > > > @@class_var ||= Regexp.new('...') > > Nope. > > :46:in `initialize': uninitialized class variable > @@httpd_combined_entry_re in Logs::Httpd::Combined::Entry (NameError) How about something like: class Thing @@var = nil def initialize @@var ||= Regexp.new('...') end end David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav