From: Trans Date: 2007-01-22T02:52:58+09:00 Subject: Re: module global variables Stefano Zacchiroli wrote: > Is there a way to define per-module global variables? My best > approximation is the following > > module Foo > @@x = 0 > def do_something_on_x > @@x = ... > end > def x > @@x > end > end > > The drawbacks are that I haven't found anything similar to > attr_reader/writer/... for instance variables. > > If interested in the real use case here it is: I've defined a module > which include a set of common functionalities which need to deliver > messages with a given verbosity. I want to avoid to pass the verbosity > configuration to all the involved class and methods but I want to avoid > to use a global $verbosity variable since the setting should be limited > to all the functionalities implemented by my module. > > Here is how I'm using the verbosity setting: > > module Foo > @@verbosity = 0 > def incr_verbosity > @@verbosity += 1 > end > def verbosity > @@verbosity > end > end > > I would love to write something like > > module Foo > @@verbosity = 0 > module_attr_reader :verbosity > def incr_verbosity > @@verbosity += 1 > end > end > > Am I using the wrong tool for my end? Try a constant. T.