Jos Backus
12/2/2004 7:21:00 AM
On Thu, Dec 02, 2004 at 01:56:49PM +0900, Michael C. Libby wrote:
> 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?
Joel's solution was what I was looking for originally (thanks Joel!) but your
solution admittedly works well too. Thanks for that, Michael.
Another approach I tried was using a class variable but that doesn't work
either, presumably because it is likewise trying to find @@debug in Debug, not
the including class:
lizzy:~% cat n
module Debug
def debug(level, msg)
puts msg unless level > @@debug
end
end
class C1
@@debug = 1
include Debug
def foo
debug(1, "this is foo")
end
end
class C2
@@debug = 1
include Debug
def bar
debug(2, "this is bar")
end
end
C1.new.foo
C2.new.bar
lizzy:~% ruby n
n:3:in `debug': uninitialized class variable @@debug in Debug (NameError)
from n:11:in `foo'
from n:23
lizzy:~%
Cheers,
--
Jos Backus _/ _/_/_/ Sunnyvale, CA
_/ _/ _/
_/ _/_/_/
_/ _/ _/ _/
jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'