[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

text file to code?

Pavel Pvl

12/17/2007 12:53:00 AM

earlier i asked if its possible to load a string as code. but now, lets
say i have a config file. a text file with ruby code in it. how can i
load that into my ruby code? but for that matter, everytime it does load
it, can it read as a new object? like lets say the config file consists
of "plugins" and i add one, can ruby pick it up while the app is
running?
--
Posted via http://www.ruby-....

2 Answers

Joel VanderWerf

12/17/2007 1:56:00 AM

0

Pavel Pvl wrote:
> earlier i asked if its possible to load a string as code. but now, lets
> say i have a config file. a text file with ruby code in it. how can i
> load that into my ruby code? but for that matter, everytime it does load
> it, can it read as a new object? like lets say the config file consists
> of "plugins" and i add one, can ruby pick it up while the app is
> running?

You can do that by wrapping the loaded code in a module, and then using
that module to access any constants or functions defined in the code.
Here's how I do that:

http://redshift.sourceforge.n...

An adaptation of this is in the facets gem, IIRC. The basic idea is
simple (suggested by Nobu Nokada):

def load_in_module(file)
module_eval(IO.read(file), File.expand_path(file))
end

This returns a module that you can use to get at stuff in the file:

m = load_in_module(file)
m::SomeClass
m.some_method

The code above assumes that the file has these definitions:

class SomeClass;...end
def self.some_method;...end

An alternative:

http://codeforpeople.com/lib/ruby...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Pavel Pvl

12/17/2007 4:29:00 AM

0

thank you so much
--
Posted via http://www.ruby-....