[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

module global variables

Stefano Zacchiroli

1/21/2007 5:32:00 PM

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.bononi...
(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

13 Answers

Trans

1/21/2007 5:53:00 PM

0


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.


Stefano Zacchiroli

1/21/2007 5:58:00 PM

0

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 :)

--
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononi...
(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

Gregory Brown

1/21/2007 6:00:00 PM

0

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?

Gregory Brown

1/21/2007 6:06:00 PM

0

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

hemant

1/21/2007 7:09:00 PM

0

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
>

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.


--
gnufied


Gregory Brown

1/21/2007 7:22:00 PM

0

On 1/21/07, Hemant Kumar <gethemant@gmail.com> wrote:

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

That's probably the same effect, and a little shorter, too.

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

Maybe you'd find this post interesting: http://tinyurl....
Not exactly the same topic, but related

hemant

1/21/2007 7:37:00 PM

0

On Mon, 2007-01-22 at 04:21 +0900, Gregory Brown wrote:
> On 1/21/07, Hemant Kumar <gethemant@gmail.com> wrote:
>
> > module A
> > @foo = "Bar"
> > class << self; attr_accessor :foo; end
> > end
> >
> > p A.foo
>
> That's probably the same effect, and a little shorter, too.
>

And i missed that, you will have to use self.class.foo to access the
module instance variable from instance methods.

> > PS : Sometimes I really need to think hard to differentiate between a
> > class and a module.
>
> Maybe you'd find this post interesting: http://tinyurl....
> Not exactly the same topic, but related
>

PS: hmm thanks.


Stefano Zacchiroli

1/21/2007 11:29:00 PM

0

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?

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.

--
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononi...
(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

Stefano Zacchiroli

1/21/2007 11:29:00 PM

0

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:

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.

--
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononi...
(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

Mike Kasick

1/22/2007 1:08:00 AM

0

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.

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.