From: Robert Klemme Date: 2009-09-08T04:20:05+09:00 Subject: Re: Opinion: Scripting in a Ruby program On 07.09.2009 18:57, Tristan Shelton wrote: > Thank you all for your input. I apologize, as it seems I caused some > confusion with my original post. I'm looking for something that allows > the user to perform a few simple operations in a sandbox context, rather > than a framework for programmers to build upon. Something that would > allow me to compile the main program with ruby2exe and then load -- and > reload without interrupting the execution of the main program -- small > bits of script from an external source (e.g. a file) with. > > To bring things into context I have a game in which I would like certain > objects to be scriptable to some degree. Nothing extremely involved; > just a few methods, properties, and control structures. So essentially a > scripting "language" that can be provided a simple subset of the main > program's functionality in a sandbox context that doesn't allow file and > network I/O, shell access, and all those other nifty things Ruby > normally does. > > Something comparable to Unrealscript or Actionscript (which as I > understand is essentially Javascript) would work fine, I'm just curious > how one would go about implementing such functionality in Ruby. Any > further suggestions are welcome, and no one solution may be best, but > I'm interested to know what's out there and what's possible. For one, you can do something like this: class Shell class Context # custom commands for user code def move puts "moving..." end end # interactive commands def load(file) c = Context.new c.instance_eval(File.read(file)) nil end end Then you can have a file which contains "move" on a line and you'll see the move printed. There is also $SAFE: http://ruby-doc.org/docs/ProgrammingRuby/html/taint.html#S1 Typically you set up an environment and then start another thread which restricts $SAFE to provide the sandbox environment. Also, Try Ruby's source should contain sandboxing techniques as well (see recent posts). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/