I’ve placed a set of .rb files in a directory modules/. Each of these files contains a module. I’m loading all of these files from a separate script with this:
Dir["modules/*.rb"].each {|file| load file }
But then I have to include them, one by one, listing and naming each of them explicitly:
class Foo
include ModuleA
include ModuleB
# ... and so on.
end
I’m wondering if there is some non-explicit one-liner that can accomplish this, a la (pseudocode) …
class Foo
for each loaded file
include the module(s) within that file
end
# ... other stuff.
end
I’ve considered actually reading the files’ contents, searching for the string “module“, and then extracting the module name, and somehow doing the include based on that — but that seems ridiculous.
Is what I’m trying to do advisable and/or possible?
You can manually define some variable in each of your file and then check for its value while including the file.
For example,
module1.rb:And the second one,
module2.rb:And then just include all of them in your file (just the idea, it may not work correctly):