Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Executing ruby code stored as a string in a database

Reply
Thread Tools

Executing ruby code stored as a string in a database

 
 
Etienne Van tonder
Guest
Posts: n/a
 
      12-28-2007
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/.

 
Reply With Quote
 
 
 
 
Tomaso Tosolini
Guest
Posts: n/a
 
      12-28-2007

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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Executing python script stored as a string Ecir Hana Python 6 09-01-2009 11:33 AM
Execute Ruby code stored in database Petr Janda Ruby 2 01-08-2008 12:54 PM
Sample code for Connecting and Executing a stored procedure is SQL server using ASP.NET Peri ASP .Net 1 06-13-2005 04:56 AM



Advertisments