Let's say I've got the following files:
|- app
| |- helpers
| | |- application_helper.rb
|- config
|- |- application.rb
|- lib
| |- my_module
| | |- my_class.rb
I'm trying to make Rails autoload my_module
. In application.rb
I set
config.autoload_paths += %W(#{config.root}/lib)
I've also managed to obtain the secret knowledge that in order for autoloading to work, the names of modules and classes must match the names of directories and files, so my_class.rb
looks like this:
module MyModule
class MyClass
# ...
end
end
Now I want to use MyClass
in my application_helper.rb
:
module ApplicationHelper
include MyModule
def some_method(active_entry = nil)
someobject = MyClass.new
#...
end
end
But I'm getting an error
uninitialized constant ApplicationHelper::MyClass
In order to make this code work I must replace
someobject = MyClass.new
with
someobject = MyModule::MyClass.new
which is ugly. I thought that the include
would work like a C++ using namespace
, C# using
or Java import
but apparently it doesn't. So is there any equivalent to the above statements in Ruby?