From: Jos Backus Date: 2004-12-02T16:26:58+09:00 Subject: Re: Accessing class constants from within a module? On Thu, Dec 02, 2004 at 02:55:04PM +0900, Joel VanderWerf wrote: > Jos Backus wrote: > >Any idea how I can access a class constant from within a module when that > >module is include'd in the class? I.o.w. how do I make this work? > > Use dynamic lookup, rather than lexical lookup: > > >module Debug > > def debug(level, msg) > > puts msg unless level > DEBUG # XXX DEBUG is wrong! > puts msg unless level > self.class::DEBUG > > end > >end It's things like these in Ruby that make my head spin :) But it does make sense: at time of invocation we want to say either C1::DEBUG or C2::DEBUG, depending on the invoking method. `::' can be preceded by any expression yielding a class, in this case self.class. This evaluates to either C1 or C2, as appropriate, and voila. Thanks guys! -- Jos Backus _/ _/_/_/ Sunnyvale, CA _/ _/ _/ _/ _/_/_/ _/ _/ _/ _/ jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'