module global variables

Discussion in 'Ruby' started by Stefano Zacchiroli, Jan 21, 2007.

  1. 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
     
    Stefano Zacchiroli, Jan 21, 2007
    #1
    1. Advertising

  2. Stefano Zacchiroli

    Trans Guest

    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.
     
    Trans, Jan 21, 2007
    #2
    1. Advertising

  3. 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.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
     
    Stefano Zacchiroli, Jan 21, 2007
    #3
  4. On 1/21/07, Stefano Zacchiroli <> 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, Jan 21, 2007
    #4
  5. On 1/21/07, Gregory Brown <> wrote:
    > On 1/21/07, Stefano Zacchiroli <> 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
     
    Gregory Brown, Jan 21, 2007
    #5
  6. Stefano Zacchiroli

    Hemant Kumar Guest

    On Mon, 2007-01-22 at 03:05 +0900, Gregory Brown wrote:
    > On 1/21/07, Gregory Brown <> wrote:
    > > On 1/21/07, Stefano Zacchiroli <> 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
     
    Hemant Kumar, Jan 21, 2007
    #6
  7. On 1/21/07, Hemant Kumar <> 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.com/25494o
    Not exactly the same topic, but related
     
    Gregory Brown, Jan 21, 2007
    #7
  8. Stefano Zacchiroli

    Hemant Kumar Guest

    On Mon, 2007-01-22 at 04:21 +0900, Gregory Brown wrote:
    > On 1/21/07, Hemant Kumar <> 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.com/25494o
    > Not exactly the same topic, but related
    >


    PS: hmm thanks.
     
    Hemant Kumar, Jan 21, 2007
    #8
  9. 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.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
     
    Stefano Zacchiroli, Jan 21, 2007
    #9
  10. 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.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
     
    Stefano Zacchiroli, Jan 21, 2007
    #10
  11. Stefano Zacchiroli

    Mike Kasick Guest

    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.
     
    Mike Kasick, Jan 22, 2007
    #11
  12. 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)?


    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.

    --
    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
     
    Stefano Zacchiroli, Jan 22, 2007
    #12
  13. Stefano Zacchiroli

    Mike Kasick Guest

    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.

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

    Thanks.
     
    Mike Kasick, Jan 23, 2007
    #13
  14. 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.


    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.

    --
    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
     
    Stefano Zacchiroli, Jan 23, 2007
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne
    Replies:
    2
    Views:
    493
    Wayne
    Nov 11, 2003
  2. jubelbrus
    Replies:
    5
    Views:
    635
    JohnQ
    Jul 20, 2007
  3. mark4asp
    Replies:
    1
    Views:
    232
  4. Tony Archer

    Global Variables? Multi-Application Variables?

    Tony Archer, Nov 24, 2003, in forum: ASP General
    Replies:
    5
    Views:
    236
    Tony Archer
    Nov 25, 2003
  5. king
    Replies:
    2
    Views:
    183
    Tad McClellan
    Jun 27, 2006
Loading...

Share This Page