hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Wednesday

2006.11.15

FreakyFreaky Now Resumes Its Usual Sandly Self #

by why in inspect

I know it’s been out of sorts for the past month, but the big checkin is, in fact, IN. The big deal today: class and module references.

 svn co http://code.whytheluckystiff.net/svn/sandbox/trunk sandbox
 cd sandbox
 ruby setup.rb

Like, say you want to give a sandbox some limited access to a database:

 require 'sandbox'
 require 'mysql'

 # setup a proxy module
 module Database
   @@db = Mysql.connect(...)
   def self.query(str)
     @@db.query(str).fetch_hash
   end
 end

 # use the proxy module inside the sandbox
 sand = Sandbox.safe
 sand.ref Database
 sand.eval("Database.query('SELECT * FROM apples')")

Well, this is a bit contrived. But, you know?

And it’s safe. In the sandbox, the Database module is actually a Database class, descended from BoxedClass, which has a method_missing which will restore the original Ruby environment, make the call, and marshal the results back to the sandbox. So the query call in the box does get its Hash.