I'm doing a web application and I want provide a "mini ruby" interpreter for users.
What a "mini ruby" mean ?
By "mini ruby" I mean a basic ruby interpreter without any context variables,
no search path for modules and classes.
possible example:
----------------
$a_globalvar = 'global value'
$b_globalvar = 'hi I'm here'
c = 'local value'
def some_func(a,b)
print "#{a} -- #{b}"
end
output = ''
my_rb = MiniRuby.new
my_rb.bind({'a' => $a_globalvar,'c' => c, 'afunc' => some_func)
my_rb.store_stdout(output);
my_rb.eval('if (c != "") && (a != "") then afunc(c,a) end')
print output # => local_value -- global_value
my_rb.eval('x = 45')
print my_rb.get('x') # => 45 or nil if not defined or throw exception
print my_rb.get('$b_globalvar') # => nil #no caller context
my_rb.eval(<<EOD
def bfunc(ap)
ap.gsub(' ', ' - ')
end
EOD
)
p = my_rb.get('bfunc')
print p('outside call') # "outside - call"
my_rm.eval('b = bfunc("hello world")')
print my_rb.get('b') # "hello - world"
my_rb.eval('require "postgres"') # throw exception
-----------
I'm sorry to don't describe it better in english than with code.
Please give comments and ideas.