Use of eval

R

Rich

I was thinking of adding a simple command console to a Ruby project
I'm working on (something like a domain-specific REPL). Under what
execution environment do the "eval"-ed strings execute, relative to
the execution environment that is calling the eval method? Can
classes be opened using eval? Finally, is there anything like Perl's
Safe module available in Ruby? Thank you.

-Rich Seagraves
 
R

Robert Klemme

Rich said:
I was thinking of adding a simple command console to a Ruby project
I'm working on (something like a domain-specific REPL). Under what
execution environment do the "eval"-ed strings execute, relative to
the execution environment that is calling the eval method?

Normally the binding of the caller is used but you can also provide a
binding explicitely:

class Test1
def test1(b)
eval("@foo = 123", b)
end
def test2()
eval("@bar = 123")
end
end
class Test2
def test1()
t = Test1.new
t.test1(binding)
[self, t]
end
def test2()
t = Test1.new
t.test2()
[self, t]
end
end

Can
classes be opened using eval?

Strings must be syntactic correct Ruby. You can of course add methods to
classes etc.
Finally, is there anything like Perl's
Safe module available in Ruby? Thank you.

Yes. There is the thread global variable $SAFE which controls what can be
done.

Kind regards

robert
 
D

David Vallner

Rich said:
I was thinking of adding a simple command console to a Ruby project
I'm working on (something like a domain-specific REPL). Under what
execution environment do the "eval"-ed strings execute, relative to
the execution environment that is calling the eval method? Can
classes be opened using eval? Finally, is there anything like Perl's
Safe module available in Ruby? Thank you.

-Rich Seagraves
You could try and reuse the already present IRB, plain eval-ing lines
the user inputs might provide some pitfalls. Just about my US$0.02

David Vallner
 

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

Similar Threads

deepcopy via eval 6
proper use of classes 31
A valid use of eval? 9
[ANN] JRuby 1.4.0 Released 2
Boids, a use case 6
use of assert in Java [vs. exceptions] 22
Why I don't use Ruby. 144
[ANN] JRuby 1.4.0RC3 Released 0

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top