gordie
gordie

Reputation: 1955

can I call a function to define a variable in a module definition?

I would like to define the variable @@importers when my module loads.

module Importers
  @@importers_dir = File.dirname(__FILE__) + '/services/'
  @@importers = self.load_and_instantiate()
  def self.load_and_instantiate()
     #mycode here
  end
end

But it does not work :

undefined method 'load_and_instantiate' for Importers:Module (NoMethodError)

How should I handle this ?

Thanks !

Upvotes: 0

Views: 40

Answers (1)

spickermann
spickermann

Reputation: 107142

At the moment you call load_and_instantiate it is indeed not defined because you define it later in the code.

Just change the order and call the method after you defined the method:

def self.load_and_instantiate
  # mycode here
end

@@importers = self.load_and_instantiate

Please note that using class variables is uncommon in Ruby.

Upvotes: 2

Related Questions