[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

eval a file

Daniel Brumbaugh Keeney

12/23/2007 8:21:00 PM

Basically, I was wondering if it is possible in Ruby to eval a file
without loading the whole thing in memory. I'm trying to avoid
eval(File.new(location).read), but at the same time, I don't want the
file to contain a named method that could conflict with other named
methods.

-Daniel Brumbaugh Keeney

2 Answers

Joel VanderWerf

12/23/2007 8:40:00 PM

0

Daniel Brumbaugh Keeney wrote:
> Basically, I was wondering if it is possible in Ruby to eval a file
> without loading the whole thing in memory. I'm trying to avoid
> eval(File.new(location).read), but at the same time, I don't want the
> file to contain a named method that could conflict with other named
> methods.

Can't help with the first part (it may not be possible, AFAIK). But the
second part is easy. Two approaches are:

http://redshift.sourceforge.n...
http://codeforpeople.com/lib/ruby...

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

Jeremy McAnally

12/23/2007 9:42:00 PM

0

Probably not the best solution, but you could load the code into an
instance of a class. For example, in a file 'test.rb':

def my_method
puts "this is my method"
end

Then in your main file (this is in irb, so you'll need to remove some stuff)...

>> class ExternalCode
>> end
=> nil
>> x = ExternalCode.new
=> #<ExternalCode:0x5b2cb4>
>> x.instance_eval(File.open('test.rb').read)
=> nil
>> x.my_method
this is my method
=> nil

--Jeremy

On Dec 23, 2007 3:21 PM, Daniel Brumbaugh Keeney
<devi.webmaster@gmail.com> wrote:
> Basically, I was wondering if it is possible in Ruby to eval a file
> without loading the whole thing in memory. I'm trying to avoid
> eval(File.new(location).read), but at the same time, I don't want the
> file to contain a named method that could conflict with other named
> methods.
>
> -Daniel Brumbaugh Keeney
>
>



--
http://www.jeremymca...

My books:
Ruby in Practice
http://www.manning.com...

My free Ruby e-book
http://www.humblelittlerub...

My blogs:
http://www.mrneigh...
http://www.rubyinpra...