Asfand Yar Qazi wrote:
> Hi,
> 
> I want to be able to do the following:
> 
> lots of Ruby files are in a directory, each containing stuff and a 
> method 'init_file'.  I want to be able to 'require' each file, and then 
> call the 'init_file' method within that file.
> 
> Each file will have its own 'init_file' method, so I can't just do a:
> 
> require 'file'
> init_file
> 
> because the init_file method will have been defined before hand.
> 
> Is it possible, like in Perl, for an included file to return a value?

You can do this using the "script" lib I just mentioned on another thread.

---- main.rb ----
require 'script'

mod1 = Script.load("file1.rb")
mod2 = Script.load("file2.rb")

[mod1, mod2].each do |mod|
   mod.init_file
   puts "The value of X for #{mod.inspect} is #{mod::X}"
end

---- file1.rb ----
def init_file
   puts "init for #{__FILE__}"
end

X = "One"

---- file2.rb ----
def init_file
   puts "init for #{__FILE__}"
end

X = "Two"

------------------

Output:
init for /tmp/script-example/file1.rb
The value of X for #<Script:/tmp/script-example/file1.rb> is One
init for /tmp/script-example/file2.rb
The value of X for #<Script:/tmp/script-example/file2.rb> is Two


Script also defines #autoscript, which you can use like #autoload to 
load the modules on demand and assign them to constants.