Run time programming

Hi all,

How can I run ruby code from my ruby program? Let's say I have a program
with a text box. In that text box user writes a ruby function. I
want the program to call that function in such way that this function
would have access to the classes and variables in the main program.

How can this be done? I imagine that since Ruby is interpreted
dynamically adding code shouldn't be much of a problem. But then I just
started learning Ruby and have no idea how this could work..

Thanks for your replies.

···

--
Posted via http://www.ruby-forum.com/.

eval(user_given_text, binding)
user_defined_method

···

--
Posted via http://www.ruby-forum.com/.

$ ri eval

Kind regards

robert

···

On Wed, May 4, 2011 at 12:39 PM, Karolis Juodele <zulupineapple@gmail.com> wrote:

How can I run ruby code from my ruby program? Let's say I have a program
with a text box. In that text box user writes a ruby function. I
want the program to call that function in such way that this function
would have access to the classes and variables in the main program.

How can this be done? I imagine that since Ruby is interpreted
dynamically adding code shouldn't be much of a problem. But then I just
started learning Ruby and have no idea how this could work..

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert K. wrote in post #996572:

$ ri eval

And see also:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/taint.html

···

--
Posted via http://www.ruby-forum.com/\.

Robert K. wrote in post #996572:

$ ri eval

And you might also want to think about who is entering that code and
what it does: cautionary tale at
http://www.ruby-doc.org/docs/ProgrammingRuby/html/taint.html

So, re-implementing tryruby.org is not as simple as you might think.
Look for the ruby sandbox gem.

If only fully trusted users are entering data into this text box, then
it's not a problem.

···

--
Posted via http://www.ruby-forum.com/\.

eval though is the root method it might be safer to use one of the
more focused wrapper methods such as class_eval, instance_eval and
define_method.

~Stu

···

On Thu, May 5, 2011 at 10:43 AM, Brian Candler <b.candler@pobox.com> wrote:

Robert K. wrote in post #996572:

$ ri eval

And you might also want to think about who is entering that code and
what it does: cautionary tale at
Programming Ruby: The Pragmatic Programmer's Guide

So, re-implementing tryruby.org is not as simple as you might think.
Look for the ruby sandbox gem.

If only fully trusted users are entering data into this text box, then
it's not a problem.

--
Posted via http://www.ruby-forum.com/\.

It's not clear to me how those are safer, I thought those just change
contexts. For example, I can still call system (or do anything else, I would
expect).

Whatever = Class.new
users_code = 'system "echo just doin the evils"'
Whatever.class_eval users_code # >> just doin the evils

···

On Thu, May 5, 2011 at 3:30 PM, Stu <stu@rubyprogrammer.net> wrote:

eval though is the root method it might be safer to use one of the
more focused wrapper methods such as class_eval, instance_eval and
define_method.

~Stu

Funny I was just playing with the go language version of tryruby which
also uses a sandbox.

I referring to how rails generators as input are used to alleviate the
boilerplate code in crud operations. For example the dynamic finders
i.e. find_by_#{evaluated_string} are most likely eval created.

I imagine putting the whole interpreter online must be a huge security
conscious effort.

···

On Thu, May 5, 2011 at 3:58 PM, Josh Cheek <josh.cheek@gmail.com> wrote:

On Thu, May 5, 2011 at 3:30 PM, Stu <stu@rubyprogrammer.net> wrote:

eval though is the root method it might be safer to use one of the
more focused wrapper methods such as class_eval, instance_eval and
define_method.

~Stu

It's not clear to me how those are safer, I thought those just change
contexts. For example, I can still call system (or do anything else, I would
expect).

Whatever = Class.new
users_code = 'system "echo just doin the evils"'
Whatever.class_eval users_code # >> just doin the evils

For example the dynamic finders
i.e. find_by_#{evaluated_string} are most likely eval created.

Sorta, yeah: method_missing which then does a class_eval to help with
performance.