Dynamically load methods from external source

Discussion in 'Ruby' started by Daniel Shackelford, Jan 10, 2007.

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

    --
    Daniel Shackelford
    Systems Administrator
    Technology Services
    Spring Arbor University
    Daniel Shackelford, Jan 10, 2007
    #1
    1. Advertising

  2. Daniel Shackelford

    Dr. D Guest

    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.


    Daniel Shackelford wrote:
    > 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.
    >
    > --
    > Daniel Shackelford
    > Systems Administrator
    > Technology Services
    > Spring Arbor University
    Dr. D, Jan 10, 2007
    #2
    1. Advertising

  3. Daniel Shackelford

    Jan Svitok Guest

    On 1/10/07, Daniel Shackelford <> wrote:
    > 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.


    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
    Jan Svitok, Jan 10, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,690
    Matt Berther
    Mar 5, 2005
  2. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,332
    Scott Allen
    Mar 4, 2005
  3. Scott Allen
    Replies:
    8
    Views:
    10,888
    Scott Allen
    May 2, 2004
  4. Kenneth McDonald
    Replies:
    5
    Views:
    288
    Kenneth McDonald
    Sep 26, 2008
  5. Replies:
    3
    Views:
    99
    Randy Webb
    Feb 9, 2007
Loading...

Share This Page