module global variables

S

Stefano Zacchiroli

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.
 
T

Trans

Stefano said:
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.
 
S

Stefano Zacchiroli

Try a constant.

Already tried. I can't stand the warning and I can't blame the compiler
for outputting it: a constant would definitely be the wrong tool :)
 
G

Gregory Brown

Already tried. I can't stand the warning and I can't blame the compiler
for outputting it: a constant would definitely be the wrong tool :)

why do you need this in your module? Is it going to be mixed into
something or no?
 
H

Hemant Kumar

Something like this might work:

=> 70

Well just a stolen idea from somewhere. But why not use module instance
variables:

module A
@foo = "Bar"
class << self; attr_accessor :foo; end
end

p A.foo


PS : Sometimes I really need to think hard to differentiate between a
class and a module.
 
S

Stefano Zacchiroli

why do you need this in your module? Is it going to be mixed into
something or no?

Actually no. It's just the "namespace" in which I'm implementing various
classes.

I can alternatively write a class pertaining to this namespace and use a
class variable of that class, but since the class would be otherwise
pointless I thought a module variable would have been a better solution
...

Cheers.
 
S

Stefano Zacchiroli

Well just a stolen idea from somewhere. But why not use module instance
variables:

Thanks (to you and Gregor), that's precisely what I was looking for!

I also found the same example in the pickaxe book (in which sometime
it's difficult to find what you were looking for from the index ...),
and now I've a clearer understanding of its meaning.

Cheers.
 
M

Mike Kasick

I also found the same example in the pickaxe book (in which sometime
it's difficult to find what you were looking for from the index ...),
and now I've a clearer understanding of its meaning.

Where is this discussed in pickaxe (which page number)?

I ran into basically the same issue a few weeks ago and came up with the
same solution. I read the pickaxe cover to cover (a long time ago) and
I couldn't remember seeing that particular example. On the other hand,
much of what I read I'm forgetting.
 
S

Stefano Zacchiroli

Where is this discussed in pickaxe (which page number)?

Page 374.

Note that the discussed topic is not "module instance variables", but
rather the class << self trick for associating a class with the self
object (which in the pickaxe case is a class object). AFAIU the same is
done in the solution proposed to me, but with a module object instead of
a class object.

Correct me if my understanding is wrong.

Cheers.
 
M

Mike Kasick

Page 374.

Hmm, must be first edition, although you gave me a clue where to look
for it. In the second edition, it's under "Class Instance Variables" in
Chapter 24: Classes and Objects, page 388.

Yeah, I guess I just don't recall reading that the first time.

Thanks.
 
S

Stefano Zacchiroli

Hmm, must be first edition, although you gave me a clue where to look
for it. In the second edition, it's under "Class Instance Variables" in
Chapter 24: Classes and Objects, page 388.

Uhm my fault. It's actually the second edition, but indeed I wanted to
point to the page you're mentioning. I probably screwed up the page
numbers between the "paper" page numbers and the pdf page numbers I was
looking at. Or something similar, since right now even the pdf page
number does not much ... But it's cool, we both found what we were
looking for :)

Cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top