From: Jonathan Wills Date: 2009-02-07T06:46:04+09:00 Subject: Re: eval statement Christopher Dicely wrote: > On Fri, Feb 6, 2009 at 11:55 AM, Jonathan Wills > wrote: >> can run ruby code. Basically I want the user to be able to supply an >> unspecified number of ruby files, all of which will get run, and not >> have to worry about namespace collisions. Additionally I want access to >> the scopes that those files were executed in after they are done. I >> know I can create a binding from my current scope, but if I put anything >> in it, it can get modified when I do my_binding.eval. > > You can get a binding from any object that has a method to tell you > its binding. Something like this (off the top of my head) might work: > --- > class ExecutionContext > private_class_method :new > > @@contexts = {} > > def self.[](key) > @@contexts[key] > end > > def self.new_binding(key) > new(key).binding > end > > def initialize(key) > raise KeyError if @@contexts[key] > @@contexts[key] = self > end > > def get_binding > binding > end > end > --- > > So, when you want a new binding to use, you call > ExecutionContext.new_binding(key), and to get back the a binding used > previously, you call ExecutionContext[key].get_binding, where "key" is > the key given when you called new_binding to generate the binding. > > But while these are mostly "fresh" bindings, they are nothing at all > like safe sandboxes. You may need something like this: > http://github.com/why/sandbox/tree/master Interesting. Someone else has shown me a similar way of getting a fresh context. Do you know what exactly about it is unlike a sandbox? I know that with this approach that code that I run can use the require statement and it will drop such files into my scope, but are there any other sort of problems? Are there any plans for ruby to support a sandboxed eval like I can get in python? I might be willing to contribute such a thing if it could get added into the standard interpreter. thanks -- Posted via http://www.ruby-forum.com/.