eval a file

  • Thread starter Daniel Brumbaugh Keeney
  • Start date
D

Daniel Brumbaugh Keeney

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
 
J

Joel VanderWerf

Daniel said:
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.net/script/
http://codeforpeople.com/lib/ruby/dynaload/
 
J

Jeremy McAnally

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)...
this is my method
=> nil

--Jeremy

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.jeremymcanally.com/

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

My free Ruby e-book
http://www.humblelittlerubybook.com/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top