Executing ruby code withing a ruby app

def run_some_script
  ruby_code = Mytable.ruby_code
  eval("ruby_code")
end

Depending on the ultimate source of that code - if you don't trust the
string completely, you might want to do:

def run_some_script
  ruby_code = Mytable.ruby_code
  Thread.new do
    $SAFE = 3
    eval(ruby_code)
  end.join
end

It will stop things like "system('del filename')" from being eval'd.

(I think that's the right way to go about it... I don't have the need to
do unsafe evals that often).

···

-----Original Message-----
From: Fernand Galiana [mailto:fernand@dim.com]
Sent: Tuesday, 11 October 2005 4:07 PM
To: ruby-talk ML
Subject: Executing ruby code withing a ruby app

Hi,

    I have some ruby code stached away in a database table. I
would like to run this code within my ruby application.
    How can I do it ?

    For example say I have the following method:

    def run_some_script
       ruby_code = Mytable.ruby_code # Where Mytable is an
ActiveRecord object that references a db table which contains
some ruby script

       # Now I want to run the ruby script that is in the db
table and return the result of the execution
    end

Thank you all for the help...

#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################

Daniel Sheppard wrote:

def run_some_script
ruby_code = Mytable.ruby_code
eval("ruby_code")
end

I guess it's just a typo but in this case it's

eval(ruby_code)

Otherwise you'll just get the contents of the variable ruby_code... :slight_smile:

Depending on the ultimate source of that code - if you don't trust the
string completely, you might want to do:

def run_some_script
ruby_code = Mytable.ruby_code
Thread.new do
$SAFE = 3
eval(ruby_code)
end.join
end

It will stop things like "system('del filename')" from being eval'd.

(I think that's the right way to go about it... I don't have the need
to
do unsafe evals that often).

Yep.

Kind regards

    robert