From: Austin Ziegler Date: 2005-02-20T22:01:11+09:00 Subject: Re: Getting a method or class within a file On Sun, 20 Feb 2005 20:54:42 +0900, Asfand Yar Qazi wrote: > Pit Capitain wrote: > > Asfand Yar Qazi schrieb: > > > >> 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. > > > > > > Not that I would recommend that, but you can do > > > > require 'file1' > > init_file > > > > require 'file2' > > init_file > > > > because the init_file method of file2 will overwrite the init_file > > method of file1. > > > > But: of course this is bad style. Where do your Ruby files come from? > > Can't you wrap the code in each file in a class or module? > > > > It's also thread-unsafe. > > You're right. I think what I'll do is to have a combination of a > Module name and a filename that will allow there to be an init method > contained within 'Module', contained within 'file'. > > But then I'll have to store 2 entries: filename and module name. But > I suppose that's ok. Perhaps I should use the following syntax: > > Module@path/to/file.rb > > to show that a module is in a certain file? It sounds like you're basically doing a plugin framework. Ruwiki uses something like this when reading its Token classes. Look at lib/ruwiki/wiki/tokens.rb to see how I do it. There is a plugin framework (FreeBASE) that may also be suitable for you in this instance. -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca