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.
···
--
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/
(15:56:48) Zack: e la demo dema ? /\ All one has to do is hit the
(15:57:15) Bac: no, la demo scema \/ right keys at the right time
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
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
···
On Mon, Jan 22, 2007 at 02:52:58AM +0900, Trans wrote:
> Am I using the wrong tool for my end?
Try a constant.
--
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/
(15:56:48) Zack: e la demo dema ? /\ All one has to do is hit the
(15:57:15) Bac: no, la demo scema \/ right keys at the right time
> > > Am I using the wrong tool for my end?
> > 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
why do you need this in your module? Is it going to be mixed into
something or no?
Something like this might work:
module A
def foo
A.bar += 10
end
module_function
def bar=(other) @bar = other
end
def bar @bar
end
end
=> nil
A.bar = 20
=> 20
class B
include A
def apple
foo
end
end
=> nil
c = B.new
=> #<B:0x31485c>
c.apple
=> 30
c.apple
=> 40
c.apple
=> 50
class C
include A
def banana
foo + 10
end
end
=> nil
d = C.new
=> #<C:0x1cc1b4>
d.banana
=> 70
d.banana
=> 80
A.bar
=> 70
···
On 1/21/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
On 1/21/07, Stefano Zacchiroli <zack@bononia.it> wrote:
> On Mon, Jan 22, 2007 at 02:52:58AM +0900, Trans wrote:
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.
···
On Mon, Jan 22, 2007 at 03:00:09AM +0900, Gregory Brown wrote:
why do you need this in your module? Is it going to be mixed into
something or no?
--
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/
(15:56:48) Zack: e la demo dema ? /\ All one has to do is hit the
(15:57:15) Bac: no, la demo scema \/ right keys at the right time
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.
···
On Mon, 2007-01-22 at 03:05 +0900, Gregory Brown wrote:
On 1/21/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 1/21/07, Stefano Zacchiroli <zack@bononia.it> wrote:
> > On Mon, Jan 22, 2007 at 02:52:58AM +0900, Trans wrote:
> > > > Am I using the wrong tool for my end?
> > > 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
>
> why do you need this in your module? Is it going to be mixed into
> something or no?
Something like this might work:
>> module A
>> def foo
>> A.bar += 10
>> end
>> module_function
>> def bar=(other)
>> @bar = other
>> end
>> def bar
>> @bar
>> end
>> end
=> nil
>> A.bar = 20
=> 20
>> class B
>> include A
>> def apple
>> foo
>> end
>> end
=> nil
>> c = B.new
=> #<B:0x31485c>
>> c.apple
=> 30
>> c.apple
=> 40
>> c.apple
=> 50
>> class C
>> include A
>> def banana
>> foo + 10
>> end
>> end
=> nil
>> d = C.new
=> #<C:0x1cc1b4>
>> d.banana
=> 70
>> d.banana
=> 80
>> A.bar
=> 70
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.
···
On Mon, Jan 22, 2007 at 04:08:38AM +0900, Hemant Kumar wrote:
Well just a stolen idea from somewhere. But why not use module instance
variables:
--
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/
(15:56:48) Zack: e la demo dema ? /\ All one has to do is hit the
(15:57:15) Bac: no, la demo scema \/ right keys at the right time
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.
···
On Mon, Jan 22, 2007 at 08:29:24AM +0900, Stefano Zacchiroli wrote:
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.
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.
···
On Mon, Jan 22, 2007 at 10:07:54AM +0900, Mike Kasick wrote:
> 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)?
--
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- stefano zacchiroli home page
(15:56:48) Zack: e la demo dema ? /\ All one has to do is hit the
(15:57:15) Bac: no, la demo scema \/ right keys at the right time
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.
···
On Mon, Jan 22, 2007 at 05:11:49PM +0900, Stefano Zacchiroli wrote:
> Where is this discussed in pickaxe (which page number)?
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.
···
On Tue, 23 Jan 2007 14:05:59 +0900, Mike Kasick wrote:
On Mon, Jan 22, 2007 at 05:11:49PM +0900, Stefano Zacchiroli wrote:
> Where is this discussed in pickaxe (which page number)?
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.
--
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/
(15:56:48) Zack: e la demo dema ? /\ All one has to do is hit the
(15:57:15) Bac: no, la demo scema \/ right keys at the right time