On Thu, 2004-12-02 at 13:00 +0900, 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? > > lizzy:~% cat m > module Debug > def debug(level, msg) > puts msg unless level > DEBUG # XXX DEBUG is wrong! > end > end > > class C1 > include Debug > DEBUG = 1 > def foo > debug(1, "this is foo") > end > end > > class C2 > include Debug > DEBUG = 2 > def bar > debug(1, "this is bar") > end > end > > C1.new.foo > C2.new.bar > lizzy:~% ruby m > m:3:in `debug': uninitialized constant Debug::DEBUG (NameError) > from m:11:in `foo' > from m:23 > lizzy:~% > You probably don't want to do that anyway. If you change to Debug::DEBUG in the class definitions, you'll be modifying a constant which can only have one value at a time. You want two different values (and you can't really tell the module to figure out which class constant is which without a lot of headaches), so maybe just set it as a default instance variable in your new objects? Here is code that seems to do what you want: # debug needs error checking in case @debug_level is nil module Debug def debug(level, msg) puts msg unless level > @debug_level end end class C1 include Debug def initialize @debug_level = 1 end def foo debug(1, "this is foo") end end class C2 include Debug def initialize @debug_level = 2 end def bar debug(1, "this is bar") end end C1.new.foo C2.new.bar -Michael Libby