Run time programming

  • Thread starter Karolis Juodele
  • Start date
K

Karolis Juodele

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

Robert Klemme

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

$ ri eval

Kind regards

robert
 
S

Stu

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
 
S

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top