Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I initialize a module's instance variables in Ruby?

I have some modules where I would like to use instance variables in. I'm currently initializing them like this:

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

I also could call a init method to initialize them:

def init   @var = 0 end 

but this would mean I have to remember to always call it.

Is there a better way of doing this?

like image 392
Geo Avatar asked Mar 30 '09 17:03

Geo


People also ask

How do you initialize an instance variable in Ruby?

You can use the method Object#instance_variables to list all instance variables of an object. You normally “declare” and initialize all the instance variables in the initialize method.

Can instance variables be initialized?

Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.

Can a Ruby module have instance variables?

Explanation: Yes, Module instance variables are present in the class when you would include them inside the class.

What is def initialize in Ruby?

The initialize method is part of the object-creation process in Ruby & it allows you to set the initial values for an object. In other programming languages they call this a “constructor”.


2 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's no mop!" MyModule.show_log #=> A red polka-dotted frog says "I like cheese."                   #   A red polka-dotted frog says "There's no mop!" 

This will set the instance variables when the module is defined. Remember, you can alwasys reopen the module later to add more instance variables and method definitions, or to redefine 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's going on?" MyModule.show_log #=> A red polka-dotted frog shouts "I like cheese."                   #   A red polka-dotted frog shouts "There's no mop!"                   #   A red polka-dotted frog shouts "What's going on?" 
like image 199
rampion Avatar answered Sep 21 '22 14:09

rampion


You can use:

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

And it will default to 0 if you don't pass anything.

If you don't want to have to call it everytime, you could use something like this:

module AppConfiguration    mattr_accessor :google_api_key    self.google_api_key = "123456789" ...  end 
like image 34
Brian Avatar answered Sep 20 '22 14:09

Brian