From: Joel VanderWerf Date: 2004-09-10T06:22:50+09:00 Subject: Re: [Design advice] including a file as code ('as is') Benny wrote: > dear list, > > I have 2 files: main.rb and include_me.rb. > I want include_me.rb to contain only the method definitions > (dont have to care about its module and class). > > [[background information: > the real problem is a bit more complicated. > finally 'main.rb' will open lots of different 'include_me.rb's in a loop > and even check if it has already opened them before open them twice, but > running classname.run each time in the loop (i.e. file name of the include > and classname are variables passed to main.rb in the loop)]] > > > the following code works, but > - will it always work no matter whats in 'include_me.rb'? > - is there a better solution to get rid of the evil "eval"? > > I know there is send(:define_method, "method_name") but I want > 'include_me.rb' to appear like if it would be a valid ruby script on its > own (without using any "special methods" only for the inclusion purpose). > > to simply redefine the same class in each include file is not an option. Take a look at http://redshift.sourceforge.net/script/ It's a variation on the load-with-wrapper suggestion and it still uses eval, but it returns the module containing all the methods and constants defined at the top-level in the loaded file. You can then call them directly or include the module in some class.