ruby: use module include in instance method of class -


have @ code below

initshared.rb module initshared   def init_shared     @shared_obj = "foobar"   end end 

myclass.rb

class myclass   def initialize()   end   def init     file_name = dir.pwd+"/initshared.rb"     if file.file?(file_name)       require file_name       include initshared       if self.respond_to?'init_shared'         init_shared         puts @shared_obj       end     end   end end 

the include initshared dosn't work since inside method .

i want check file , include module , access variables in module.

instead of using samnang's

singleton_class.send(:include, initshared) 

you can use

extend initshared 

it same, version independent. include module objects own singleton class.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -