# Processing code file inline

**URL:** <https://www.ruby-forum.com/t/processing-code-file-inline/145130>\
**Category:** Ruby\
**Created:** [August 6, 2008, 9:37am UTC](https://www.ruby-forum.com/t/processing-code-file-inline/145130 "2008-08-06T09:37:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [August 6, 2008, 9:37am UTC](https://www.ruby-forum.com/t/processing-code-file-inline/145130/1 "2008-08-06T09:37:52Z")

</div>

I need to have a script load a file and execute that file as if it was  
code inline to the script. The file being loaded is not a class, not a  
module. It is code that has been dynamically written by another process.

Example:

# file to load

index\_33dob.store(“NatVir20000515”.to\_sym, 67)  
index\_33dob.store(“MicSwa19920210”.to\_sym, 85)  
index\_33dob.store(“AshSmi19950827”.to\_sym, 19)  
index\_33dob.store(“IvaCov20021108”.to\_sym, 45)  
index\_33dob.store(“CatOrt19950128”.to\_sym, 83)

# contrived script to load the file and process it

timer\_start = Time.new  
index\_33dob = {}  
include “33dob\_entries.rb” # \<\<— WHAT DO HERE ?  
timer\_stop = Time.new  
puts “#{timer\_stop - timer\_start}”  
p index\_33dob

In other languages I have used, the command include would be used to  
integrate the 33dob\_entries.rb file as inline code, but neither Ruby’s  
include nor require can be used to do this AFAICT.

How would I do this with Ruby ?

(I am writing some data structures to disk as code instead of using  
Marshal because it is actually much much faster than reconstituing the  
data via Marshal, and that re-loading performance is critical to the  
app).

thx

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [August 6, 2008, 9:45am UTC](https://www.ruby-forum.com/t/processing-code-file-inline/145130/2 "2008-08-06T09:45:00Z")

</div>

Dang, thought of one more thing right after I sent this and it worked.

I needed to use @index\_33dob instead of index\_33dob in order to have the  
var from the loaded file available to the script.

– gw

Greg W. wrote:

> I need to have a script load a file and execute that file as if it was  
> code inline to the script. The file being loaded is not a class, not a  
> module. It is code that has been dynamically written by another process.
> 
> Example:
> 
> # file to load
> 
> index\_33dob.store(“NatVir20000515”.to\_sym, 67)  
> index\_33dob.store(“MicSwa19920210”.to\_sym, 85)  
> index\_33dob.store(“AshSmi19950827”.to\_sym, 19)  
> index\_33dob.store(“IvaCov20021108”.to\_sym, 45)  
> index\_33dob.store(“CatOrt19950128”.to\_sym, 83)
> 
> # contrived script to load the file and process it
> 
> timer\_start = Time.new  
> index\_33dob = {}  
> include “33dob\_entries.rb” # \<\<— WHAT DO HERE ?  
> timer\_stop = Time.new  
> puts “#{timer\_stop - timer\_start}”  
> p index\_33dob
> 
> In other languages I have used, the command include would be used to  
> integrate the 33dob\_entries.rb file as inline code, but neither Ruby’s  
> include nor require can be used to do this AFAICT.
> 
> How would I do this with Ruby ?
> 
> (I am writing some data structures to disk as code instead of using  
> Marshal because it is actually much much faster than reconstituing the  
> data via Marshal, and that re-loading performance is critical to the  
> app).
> 
> thx
> 
> – gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [August 6, 2008, 9:48am UTC](https://www.ruby-forum.com/t/processing-code-file-inline/145130/3 "2008-08-06T09:48:17Z")

</div>

2008/8/6 Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> # contrived script to load the file and process it
> 
> timer\_start = Time.new  
> index\_33dob = {}  
> include “33dob\_entries.rb” # \<\<— WHAT DO HERE ?  
> timer\_stop = Time.new  
> puts “#{timer\_stop - timer\_start}”  
> p index\_33dob

Read the file into a string, and then use Kernel#eval. Just a warning  
that this can be very unsafe if you don’t trust the processes  
generating the code.

Regards,  
Farrel
