How to autoload a class in ruby -


i have module follows

-path of class myclass lib/a/b/myclass.rb

module a; module b  class myclass  puts 'inside myclass'  end end end; 

now want autoload above class file in root directory. file name : dostuff

def main   autoload a::b:myclass,'a/b/myclass.rb' #path correct , getting error here   c = a::b:myclass.new end  main 

am getting error: uninitialized constant a::b::myclass (nameerror)

if use require follows , delete autoload code works fins.

require 'a/b/myclass'  

you're asking of auto-loader. can deal 1 level @ time. means need expressing each module or class in separate file:

# a.rb module   autoload(:b, 'a/b') end  # a/b.rb module a::b   autoload(:myclass, 'a/b/my_class') end  # a/b/my_class.rb class a::b::myclass end 

then can auto-load a:

autoload(:a, 'a')  a::b::myclass.new 

it's highly unconventional have main function in ruby. put code @ top level in context called main.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -