How to initialize module instance variables in Ruby?

I have some modules in which I would like to use instance variables. I am currently initializing them as follows:

module MyModule def self.method_a(param) @var ||= 0 # other logic goes here end end 

I could also call the init method to initialize them:

 def init @var = 0 end 

but that will mean that I must remember, to always call him.

Is there a better way to do this?

+71
ruby module instance-variables
Mar 30 '09 at 17:56
source share
5 answers

Initialize them in the module definition.

 module MyModule # self here is MyModule @species = "frog" @color = "red polka-dotted" @log = [] def self.log(msg) # self here is still MyModule, so the instance variables are still available @log << msg end def self.show_log puts @log.map { |m| "A #@color #@species says #{m.inspect}" } end end MyModule.log "I like cheese." MyModule.log "There no mop!" MyModule.show_log #=> A red polka-dotted frog says "I like cheese." # A red polka-dotted frog says "There no mop!" 

This sets the instance variables when the module is defined. Remember that you can reopen the module later to add additional instance variables and method definitions, or to override existing ones:

 # continued from above... module MyModule @verb = "shouts" def self.show_log puts @log.map { |m| "A #@color #@species #@verb #{m.inspect}" } end end MyModule.log "What going on?" MyModule.show_log #=> A red polka-dotted frog shouts "I like cheese." # A red polka-dotted frog shouts "There no mop!" # A red polka-dotted frog shouts "What going on?" 
+96
Mar 30 '09 at 19:42
source share

You can use:

 def init(var=0) @var = var end 

And the default will be 0 if you don't pass anything.

If you don't want to call it every time, you can use something like this:

 module AppConfiguration mattr_accessor :google_api_key self.google_api_key = "123456789" ... end 
+6
Mar 30 '09 at 18:03
source share

for a class, I would say the following, since initialization is called whenever you .new a new instance of the class.

 def initialize @var = 0 end 

from Practical ruby :

It is further stated that the initialization module will be called if, including the initialization calls of the super class, but does not mention that this is a consequence of how the super works everywhere and not the initialization. (Why can we assume Initialization receives special handling? Because it receives special with respect to visibility. Special cases create confusion.)

+2
Mar 30 '09 at 18:12
source share

This seems to be a bad form for initializing instance variables in a module in Ruby. (For reasons that I do not quite understand, but refer to the order in which things are created.)

It seems that the best practice is to use accessories with lazy initialization, for example:

 module MyModule def var @var ||= 0 end end 

Then use var as the recipient for @var .

+2
Apr 15 '14 at 12:14
source share

I answered a similar question, you can set class instance variables by doing this

 module MyModule class << self; attr_accessor :var; end end MyModule.var => nil MyModule.var = 'this is saved at @var' => "this is saved at @var" MyModule.var => "this is saved at @var" 
+1
Jan 09 '12 at 5:13
source share



All Articles