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