rVM

rVM is the implementation of a VirtualMachine in pure Ruby. To clarify, no virtual machine as in VMWare that lets you virtualize a computer. But rather like ruby 1.9 or JRuby that both implement a virtual machine (written in C or Java) to execute Ruby code. Just that rVM takes it the other way around, it is written in ruby and lets you execute code within ruby.

So what is so cool about that?

For once, the challenge to pull this off ;P. Then it has on huge advantage. It makes it pretty easy to give ruby programs a interface for scripting.

Now you might think ‘wow cool but I could just use ruby!’ that is right, and wrong. Imagine just short code snippets to make a query, perhaps you want to implement a SQL based language to let a user query data in your program? Also consider you want your website to allow user side scripts. I would not want to have ruby code executed for random visitors. Then again I might be more tempted to let them execute code within a VM that has no access to anything outside it’s VM neither variables nor hardware nor memory.

Example

The following example implements a pretty easy calculator using the the math language shipped with rVM. Set variables and are not used outside one line

require 'rubygems'
require 'amber' # This loads the rVM gem
require 'amber/languages/math'  #This loads the math plugin as well as the math function library
s =''
compiler = AmberVM::Languages[:math].new #Lets get us the compiler
while s!= 'QUIT'
  s=gets.chomp
  puts compiler.compile(s).execute(compiler.env) # We pass an fresh environment as we don't need an environment.
end

Here one example that keeps set variables due to using one environment for all the calls.

require 'rubygems'
require 'amber' # This loads the rVM gem
require 'amber/languages/math'  #This loads the math plugin as well as the math function library
s =''
compiler = AmberVM::Languages[:math].new #Lets get us the compiler
env = compiler.env
while s!= 'QUIT'
  s=gets.chomp
  puts compiler.compile(s).execute(env) # We pass an fresh environment as we don't need an environment.
end