I'm not sure what would be the best way to accomplish the following in
ruby:
I'm writing a program that periodically looks for files in a certain
directory. If it finds new ones, I want it to ...
1. Dynamically create a new module whose name is based somehow upon the
name of the file.
2. Do a Kernel#load on the file within this newly created module.
3. Invoke a method called #init that is defined in this file (assuming
that this method is actually defined), and do the invocation within
the context of that newly-created module.
For example, suppose my directory contains the following files:
foo.module
···
----------
def init
puts "invoking #init within #{self}"
end
bar.module
----------
def init
puts "invoking #init within #{self}"
end
When my program encounters the "foo.module" file, I'd like it to do the
following:
- create a module called Foo (the name of the file with ".module"
stripped off and the first letter capitalized) ... or maybe
SomethingElse::Foo, to avoid collisions with existing packages
- do a load() of the "foo.module" file within the context of this
newly created Foo module (or SomethingElse::Foo module)
- check to see if the #init method is now defined within the
Foo module; if so, invoke that as a class method within the
context of that module (i.e., as if it were invoked like
Foo#init); this should cause the following message to
print:
invoking #init within Foo
What is the recommended way for accomplishing all this within Ruby?
--
Lloyd Zusman
ljz@asfast.com
God bless you.