Executing ruby code stored as a string in a database

Discussion in 'Ruby' started by Etienne Van tonder, Dec 28, 2007.

  1. I'm trying to retrieve some ruby code from a database as a string, I
    would then like to execute this code. So far I've tried many approaches
    but my latest uses define_method.

    I have a method defined to use define_method, i.e.

    def define_validation_method(name, &block)
    self.class.send:)define_method, name, &block)
    end

    What I would like to do is pass the code that I want executed to this
    method as an example:

    function_module = define_validation_method(
    :validate_object)
    "do |x, y, z|
    if x > 1
    x = y + z
    else
    x = y - z
    end
    end"

    This of course does not work as the method expects a block not a string.
    Can anyone suggest how to get this method to work, or suggest another
    method. The code is being executed within a ruby class.

    Thanks.
    --
    Posted via http://www.ruby-forum.com/.
    Etienne Van tonder, Dec 28, 2007
    #1
    1. Advertising

  2. Hi
    have you tried with class_eval? when you have to execute ruby code contained
    in a string it should be fine for you

    def define_validation_method(name, method_body)
    self.class.class_eval("

    def #{name}
    #{method_body}
    end

    ")
    end

    Hope this helps
    Tom


    Etienne Van tonder wrote:

    > I'm trying to retrieve some ruby code from a database as a string, I
    > would then like to execute this code. So far I've tried many approaches
    > but my latest uses define_method.
    >
    > I have a method defined to use define_method, i.e.
    >
    > def define_validation_method(name, &block)
    > self.class.send:)define_method, name, &block)
    > end
    >
    > What I would like to do is pass the code that I want executed to this
    > method as an example:
    >
    > function_module = define_validation_method(
    > :validate_object)
    > "do |x, y, z|
    > if x > 1
    > x = y + z
    > else
    > x = y - z
    > end
    > end"
    >
    > This of course does not work as the method expects a block not a string.
    > Can anyone suggest how to get this method to work, or suggest another
    > method. The code is being executed within a ruby class.
    >
    > Thanks.
    Tomaso Tosolini, Dec 28, 2007
    #2
    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. sean
    Replies:
    0
    Views:
    306
  2. Peri
    Replies:
    1
    Views:
    399
    Patrick Olurotimi Ige
    Jun 13, 2005
  3. Ecir Hana
    Replies:
    6
    Views:
    483
    Hendrik van Rooyen
    Sep 1, 2009
  4. Fernand Galiana

    Executing ruby code withing a ruby app

    Fernand Galiana, Oct 11, 2005, in forum: Ruby
    Replies:
    0
    Views:
    87
    Fernand Galiana
    Oct 11, 2005
  5. Petr Janda
    Replies:
    2
    Views:
    139
    John Joyce
    Jan 8, 2008
Loading...

Share This Page