# Eval a file

**URL:** <https://www.ruby-forum.com/t/eval-a-file/124035>\
**Category:** Ruby\
**Created:** [December 23, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/eval-a-file/124035 "2007-12-23T21:22:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [December 23, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/eval-a-file/124035/1 "2007-12-23T21:22:07Z")

</div>

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 K.

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [December 23, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/eval-a-file/124035/2 "2007-12-23T21:40:18Z")

</div>

Daniel Brumbaugh K. 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.net/script/](http://redshift.sourceforge.net/script/)  
[http://codeforpeople.com/lib/ruby/dynaload/](http://codeforpeople.com/lib/ruby/dynaload/)

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [December 23, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/eval-a-file/124035/3 "2007-12-23T22:43:01Z")

</div>

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 K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 K.

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My books:  
Ruby in Practice

> **[Ruby in Practice](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...

My free Ruby e-book

> **[Site not found · DreamHost](http://www.humblelittlerubybook.com/)**
>
> The owner of this domain has not yet uploaded their website.

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)
