First, we need to put some restriction:
- Every file script_my1.rb will have the module named Script_my1. I.e. first letter capitalized, all other letters - lowercase.
Create two files script_my1.rb and script_my2.rb as follows:
---script_my1.rb:
module Script_my1
@value = 0
def self.some_procedure(i)
puts "#{i} my1 executed!"
@value = i
end
def self.another_procedure()
return @value
end
end
---script_my2.rb:
module Script_my2
@value = 0
def self.some_procedure(i)
puts "#{i} my2 executed!"
@value = i
end
def self.another_procedure()
return @value
end
end
Now the main script, that loads and executes some_procedure() in each module, and then another_procedure().
Please notice, that each module can have separated variables with the same name @value.
Moreover, I think every module can be executed in a separate thread and have access to global variables, but I have not tested it yet.
---main.rb:
# Load all files from the current directory
# with name like script_xxx.rb
i = 1
result = nil
Dir['./script_*.rb'].each { |f|
next if File.directory?(f)
require (f)
moduleName = f[2,f.length].rpartition('.rb')[0].capitalize
eval ( "#{moduleName}.some_procedure(%d)" % i )
eval ( "result = #{moduleName}.another_procedure()" )
puts result
i = i + 1
}
Output of this program is:
1 my1 executed!
1
2 my2 executed!
2
That is all!