# Dynamically load methods from external source

**URL:** <https://www.ruby-forum.com/t/dynamically-load-methods-from-external-source/81617>\
**Category:** Ruby\
**Created:** [January 10, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/dynamically-load-methods-from-external-source/81617 "2007-01-10T17:51:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [January 10, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/dynamically-load-methods-from-external-source/81617/1 "2007-01-10T17:51:51Z")

</div>

I know this sounds like it could be a very bad thing to do, but I was  
wondering if there is a way to load methods into an already running app  
from an external source (database/file). I am building an app that  
would load needed code snippets for a given task, but each time it runs  
it will not need all the snippets, just certain ones. I know that I can  
store the code as text in the database/file, but will I have to execute  
it externally (via system ‘ruby file.rb arg1 arg2’), or can I load and  
execute it internally (possibly in a separate thread)? I can think of  
all sorts of reasons why to NOT do this, but still, there is curiosity  
isn’t there…

I am thinking that somehow Rails does something like this with it’s  
controllers, but heck, that is a lot of code to sort through.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [January 10, 2007, 6:24pm UTC](https://www.ruby-forum.com/t/dynamically-load-methods-from-external-source/81617/2 "2007-01-10T18:24:58Z")

</div>

I have a long running ruby process which does just this. There’s a drb  
port open on the process. If a method is changed, I open an irb  
session, connect to drb, and run “load” on the remote process to reload  
the file containing the changed methods. To reduce the typing I have  
an rload method in the object exposed by drb.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [January 10, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/dynamically-load-methods-from-external-source/81617/3 "2007-01-10T18:25:58Z")

</div>

On 1/10/07, Daniel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am thinking that somehow Rails does something like this with it’s  
> controllers, but heck, that is a lot of code to sort through.

use Kernel#eval for loading\* code from string,  
load/require to load code from file.  
(require will load just once, load will more times)

loading = load and execute
