# Execute Ruby code stored in database

**URL:** <https://www.ruby-forum.com/t/execute-ruby-code-stored-in-database/125162>\
**Category:** Ruby\
**Created:** [January 6, 2008, 2:01pm UTC](https://www.ruby-forum.com/t/execute-ruby-code-stored-in-database/125162 "2008-01-06T14:01:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [January 6, 2008, 2:01pm UTC](https://www.ruby-forum.com/t/execute-ruby-code-stored-in-database/125162/1 "2008-01-06T14:01:48Z")

</div>

Hi,  
I’ve got a hypothetical question, and im hoping someone can answer it.  
Let’s say ive got a MySql server and I store some Ruby code inside a  
column/table/database. Is there anyway I can load this data from a Ruby  
program and execute the code as if it was a part of the program?

Cheers,  
Petr

---

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [January 6, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/execute-ruby-code-stored-in-database/125162/2 "2008-01-06T14:14:52Z")

</div>

On Jan 6, 2008 6:31 PM, Petr J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> I’ve got a hypothetical question, and im hoping someone can answer it.  
> Let’s say ive got a MySql server and I store some Ruby code inside a  
> column/table/database. Is there anyway I can load this data from a Ruby  
> program and execute the code as if it was a part of the program?

One obvious way is to use ‘eval’

–  
Let them talk of their oriental summer climes of everlasting  
conservatories; give me the privilege of making my own summer with my  
own coals.

[http://gnufied.org](http://gnufied.org)

---

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [January 8, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/execute-ruby-code-stored-in-database/125162/3 "2008-01-08T13:55:01Z")

</div>

On Jan 6, 2008, at 7:01 AM, Petr J. wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

There could be a few ways to do this.  
One method would be actually loading that text to a string, and the  
string having a file name like name. Then use try to “load  
that\_string.rb”  
No idea if that would work.

The method that would work for sure, get data from DB, save it as  
a .rb text file, “load” the file. Of course, that’s pointless if you  
have to do all of that.

If you just want snippets of code, use eval on the snippets loaded  
from DB.

Generally speaking, the file system of the OS is going to be faster  
than any DB.
