Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2171

Using variables in modules

From Stefan Salewski <mail@ssalewski.de>
Newsgroups comp.lang.ruby
Subject Using variables in modules
Date 2011-04-02 19:30 -0500
Organization Service de news de lacave.net
Message-ID <1301790250.3173.40.camel@AMD64X2.fritz.box> (permalink)

Show all headers | View raw


Can I define a variable in a module, and access and redefine it later?
Something like

module Gravity

G = 9.81

end

puts Gravity::G

Gravity::G = 9.8102 # we have done a more precise measurement

puts Gravity::G

works, but gives a warning. I have done some Google search and tried
instance and class variables for that module, but it does not work. My
goal: I have a module named Config with a configuration hash, with
predefined colors. I access that hash from other modules. That hash
should have default values, but it should be possible to redefine it.
(The other modules, which access that hash, are independent of each
other, none of then is special, so it is not really a good idea if one
of them has to define the initial hash content.) Currently I am using a
global variable for this purpose, called something like $Config_Colors.
Works fine, but I think I should use something related to my
configuration module, like Config::colors.

Best regards,

Stefan Salewski


Back to comp.lang.ruby | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Using variables in modules Stefan Salewski <mail@ssalewski.de> - 2011-04-02 19:30 -0500
  Re: Using variables in modules Stefan Salewski <mail@ssalewski.de> - 2011-04-02 19:57 -0500
    Re: Using variables in modules Stefano Crocco <stefano.crocco@alice.it> - 2011-04-03 01:39 -0500
  Re: Using variables in modules Brian Candler <b.candler@pobox.com> - 2011-04-03 02:55 -0500
  Re: Using variables in modules spiralofhope <spiralofhope_rubyml@lavabit.com> - 2011-04-03 12:37 -0500
    Re: Using variables in modules Stefan Salewski <mail@ssalewski.de> - 2011-04-03 15:29 -0500
    Re: Using variables in modules Brian Candler <b.candler@pobox.com> - 2011-04-03 15:30 -0500

csiph-web