From: Robert Klemme Date: 2004-09-16T01:19:53+09:00 Subject: Re: Truly modular "Jesse van den Kieboom" schrieb im Newsbeitrag news:1095262552.10900.12.camel@ayla... > Hi, > > As Ruby claims to be very modular I'd like to do the following (I tried > some things but I think I'm going in the wrong directions). > > I have my main Ruby application. The applications need to be extendible. > My idea is to write extra .rb files and put them in one directory. The > main app should somehow load those files to extend functionality. on > runtime the files can changes and the main app should be able to reload > the extensions. Also new files can be added and these should be reloaded > too. > > I don't know the right approach to this problem. The external .rb files > somehow need to have access to parts of the main application and the > main application obviously needs to call functions from the external > files. > > Users can communicatie with the main app telling it there are new > external files to be loaded, the name of the files and maybe function > names to be called or whatever. The idea is that I don't want to restart > the main application but do want to extend it on the flow. > > Somebody with some great ideas how to achieve this? Great? Dunno... As far as I can see here are two problems: i) file loading with respect to changes ii) communication between the code in the files and your app I can suggest something for i) (see below) but for ii) I'd have to know more about the nature of the app. Kind regards robert require 'singleton' class ExtensionManager include Singleton def initialize @files = {} end def refresh @files.dup.each do |file, time| load_file(file) if File.mtime(file) > time end self end def load_file(file) load(file) @files[file] = Time.now self end def maybe_load(file) load_time = @files[file] load_file(file) if load_time.nil? || File.mtime(file) > load_time self end end ExtensionManager.instance.maybe_load( "test.rb" ) ExtensionManager.instance.maybe_load( "test.rb" ) sleep 2 File.open("test.rb", "a") {|io| io.puts("")} ExtensionManager.instance.refresh # ExtensionManager.instance.maybe_load( "test.rb" ) # ExtensionManager.instance.maybe_load( "test.rb" )