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


Groups > comp.lang.ruby > #2171 > unrolled thread

Using variables in modules

Started byStefan Salewski <mail@ssalewski.de>
First post2011-04-02 19:30 -0500
Last post2011-04-03 15:30 -0500
Articles 7 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  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

#2171 — Using variables in modules

FromStefan Salewski <mail@ssalewski.de>
Date2011-04-02 19:30 -0500
SubjectUsing variables in modules
Message-ID<1301790250.3173.40.camel@AMD64X2.fritz.box>
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


[toc] | [next] | [standalone]


#2172

FromStefan Salewski <mail@ssalewski.de>
Date2011-04-02 19:57 -0500
Message-ID<1301792211.3173.46.camel@AMD64X2.fritz.box>
In reply to#2171
On Sun, 2011-04-03 at 09:30 +0900, Stefan Salewski wrote:
> Can I define a variable in a module, and access and redefine it later?

OK, this is very close to my desire:

module Gravity

#def initialize()
  @g = 9.81
#end

def self.get()
  @g
end

def self.set(g)
  @g = g
end

end

puts Gravity::get()

Gravity.set(9.8102) # we have done a more precice measurement

puts Gravity.get()

This gives output
stefan@AMD64X2 ~/pet $ ruby hhh.rb 
9.81
9.8102

Is there something like attr_accessor for modules, allowing writing
something like g=9.8102 and puts g instead of set and get methods?


[toc] | [prev] | [next] | [standalone]


#2181

FromStefano Crocco <stefano.crocco@alice.it>
Date2011-04-03 01:39 -0500
Message-ID<4D498EF305127BBA@smtp209.alice.it>
In reply to#2172
On Sunday 03 April 2011 09:57:05 Stefan Salewski wrote:
> On Sun, 2011-04-03 at 09:30 +0900, Stefan Salewski wrote:
> > Can I define a variable in a module, and access and redefine it later?
> 
> OK, this is very close to my desire:
> 
> module Gravity
> 
> #def initialize()
>   @g = 9.81
> #end
> 
> def self.get()
>   @g
> end
> 
> def self.set(g)
>   @g = g
> end
> 
> end
> 
> puts Gravity::get()
> 
> Gravity.set(9.8102) # we have done a more precice measurement
> 
> puts Gravity.get()
> 
> This gives output
> stefan@AMD64X2 ~/pet $ ruby hhh.rb
> 9.81
> 9.8102
> 
> Is there something like attr_accessor for modules, allowing writing
> something like g=9.8102 and puts g instead of set and get methods?

You don't need to use attr_accessor or attr_writer to define methods ending 
with an =:

module Gravity

 @g = 9.81

 def self.g= value
  @g = value
 end

 def self.g
  @g
 end

end

If you want to use attr_accessor, you'll need to do so from the singleton 
class of Gravity:

module Gravity

  class << self
    attr_accessor :g
  end

end

I hope this helps

Stefano

[toc] | [prev] | [next] | [standalone]


#2185

FromBrian Candler <b.candler@pobox.com>
Date2011-04-03 02:55 -0500
Message-ID<42edceffd89bb95234be06fa427ccd18@ruby-forum.com>
In reply to#2171
Stefan Salewski wrote in post #990601:
> 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.

If you want to do this with a constant then you probably want 
Hash#replace:

Config::Colors = {:red => 1, :blue => 2}
Config::Colors.replace({:red => 3, :blue => 4})

You'll get no warning because the constant still points to the same 
object, you've just mutated that object. But as others have said, a 
class instance variable is probably cleaner.

Hash#merge is useful too when you have defaults, so unspecified keys 
retain their default values:

def configure(settings)
  Config::Colors.replace(Config::Defaults.merge(settings))
end

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2200

Fromspiralofhope <spiralofhope_rubyml@lavabit.com>
Date2011-04-03 12:37 -0500
Message-ID<20110403103615.1068791b@user-GA-MA785GM-US2H>
In reply to#2171
On Sun, 3 Apr 2011 09:30:28 +0900
Stefan Salewski <mail@ssalewski.de> wrote:

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

You're not defining a variable.  You're defining a constant with the
capital G.  Use lowercases and this should work just fine.

[toc] | [prev] | [next] | [standalone]


#2213

FromStefan Salewski <mail@ssalewski.de>
Date2011-04-03 15:29 -0500
Message-ID<1301862550.2788.4.camel@AMD64X2.fritz.box>
In reply to#2200
On Mon, 2011-04-04 at 02:37 +0900, spiralofhope wrote:

> You're not defining a variable.  You're defining a constant with the
> capital G.  Use lowercases and this should work just fine.
> 

I know.

Please note that lower case letter will not work in this context:

module Gravity
g = 9.81 
end
puts Gravity::g
Gravity::g = 9.8102
puts Gravity::g

>iii.rb:4: undefined method `g' for Gravity:Module (NoMethodError)

Brian Candler and Stefano Crocco already answered my question, thanks.


[toc] | [prev] | [next] | [standalone]


#2214

FromBrian Candler <b.candler@pobox.com>
Date2011-04-03 15:30 -0500
Message-ID<99e0c41327bc69f08a043f08d9fd615c@ruby-forum.com>
In reply to#2200
spiralofhope wrote in post #990694:
> On Sun, 3 Apr 2011 09:30:28 +0900
> Stefan Salewski <mail@ssalewski.de> wrote:
>
>>
>> Gravity::G = 9.8102 # we have done a more precise measurement
>>
>> puts Gravity::G
>>
>> works, but gives a warning.
>
> You're not defining a variable.  You're defining a constant with the
> capital G.  Use lowercases and this should work just fine.

Before handing out advice I suggest you test it first. Hint: if you just 
change "G" to "g" in his original code, it will not work.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web