Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
module global variables
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Stefano Zacchiroli, post: 4545349"] 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? Many thanks in advance, Cheers. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
module global variables
Top