I have an external file: path_to_external_file.rb with some class definition:
class A
some_definitions
end
And I want to load that within module B so that the class A defined above can be referred to as B::A. I tried:
class B
load('path_to_external_file.rb')
end
but A is defined in the main environment, not in B:
A #=> A
B.constants # => []
How can I load external files within some class/module?
Edit
Should I read the external files as strings, and evaluate them within Class.new{...}, and include that class within B?
You cannot. At least using
loadorrequire, the Ruby files will always be evaluated in a top context.You can work around that problem in two ways:
class B::Adirectly (but you are probably trying to avoid that)eval(File.read("path_to_external_file.rb"))within yourBclassEdit: Maybe, this library is interesting for you: https://github.com/dreamcat4/script/blob/master/intro.txt