Why does Scriptroute use Ruby?
Neil Spring
Abstract:
Scriptroute is a network measurement system composed of a
privileged daemon and a safe-mode Ruby interpreter. This
note explains my choice of Ruby and presents a starting
point for discussions on whether to settle on Ruby or seek
another language with different features.
1 Introduction
My choice of
Ruby as a
scripting language for
Scriptroute
measurements has proven controversial. These discussions
surprise me because choosing a language wasn't without
precedent: the choice of Tcl for the network simulator, ns,
has made the research
community learn Tcl. And Ruby is far easier to learn than Tcl.
Ruby was the right choice for Scriptroute, even though it is
not as widely known as Perl, as widely supported with
external libraries as Python, as trusted in safe systems as
Modula-3, or as flexible as Java.
The choice of interpreted language doesn't really matter, so
feel free to argue with me or propose an alternative. There is
a narrow interface between the interpreter and the daemon,
and this interface can be accessed from other languages.
When
first working on Scriptroute, I convinced my advisors, David
Wetherall and Tom Anderson, that we could choose the wrong
language and still have a valuable system: all of the
interesting research was in the philosophy of untrusted code
and in the daemon that enforced policy. I built into
the architecture the possibility that a better language
might come along: Scriptroute clients ask for srrubycgi as
the CGI script to execute to allow
sr[something-else]cgi to exist. If the
Scriptroute daemon becomes the preferred mechanism for
allowing users to measure their own networks, replacing
set-uid programs as I hope it will, I expect others will
build interfaces to other languages.
As Scriptroute grows and becomes popular, more people are
affected by the choice of Ruby. This sketch of a document
is an argument that the decision was not taken lightly and
that your effort in learning Ruby is not wasted.
2 Caveats
I hope someday to propose Scriptroute-enhanced Ruby as a
lingua franca for active network measurement. However, this
note does not make the case either that a single language
should be chosen or that Ruby is the only language for the
task. This note is only a starting point for that discussion.
There are several good language comparisons on-line. This
is not one of them. This note does not assert that your
favorite language lacks some feature: most languages will
allow the programmer to kludge a feature even if it isn't
part of the original design of the language. That's what
function pointers are for.
My philosophy is that learning new languages is fun, and
that junior-level undergraduates should have no trouble
becoming proficient in Ruby.
3 Why Ruby
3.1 Crucial features
Ruby has a flexible safe mode. A Scriptroute script must be
able to write results back to the client, perhaps interact
with the runtime to inject packets and lookup a name or two,
but not open files or fork threads.
Ruby can be embedded in C. For safety, the Scriptroute
environment can't be just a native language module linked by
the interpreter. Resource limits and the connection to the
local daemon must be set up before the safe mode is entered
and the script is run. To do this, Scriptroute embeds
Ruby---the Ruby interpreter's functionality is implemented
in a library that can be linked by the Scriptroute
interpreter.
3.2 Niceties
Ruby is designed as an object oriented language. At first I
thought this just meant that creating a hash table of arrays
is not a kludge. I realized later that this means that it
is easy for one script to extend and modify another through
inheritance and encapsulation. I've used these features
when merging traceroute code with bandwidth probing code and
name-decoding code.
Ruby has a rich standard library. Like Java or Perl, you
don't have to implement lists or hashes. Unlike Python,
regular expressions are literals in the language.
Ruby supports anonymous functions. I've grown to dislike
for loops when
map will work just as well.
Sometimes the best parameter is
code, and iterating through a data structure makes more sense
to me with lambdas.
Ruby's speed is comparable to that of other scripting
languages. See The Great Computer Language
Shootout.
That the speed is comparable is nice, but not required for
Scriptroute.
I had also seen a comparison of Python
and
Ruby
which suggested that Ruby was cleaner and easier to learn
for those who knew neither.
Ruby is infectious. Once you stop writing semicolons, you
will not want to go back. Once you stop writing
@{$foo{$asn}->{"$prefix/$prefixlen"}}
and other
unclean constructions, you cannot go back.
4 Why Not Ruby
In the interest of fairness, there are weaknesses in Ruby,
some of which are acknowledged by the Ruby community and
being actively worked on.
Ruby does not allow named arguments to be reordered (unlike Python).
Ruby is dynamically typed, like most (all?) scripting
languages. I like static validation and use
splint
when I can in my C code.
Ruby has relatively few external libraries. While good
libraries exist for parsing XML and many other tasks, the
depth of extension interfaces for Python and Perl dwarfs
that of Ruby. This lack of external libraries is a problem
for tasks such as statistical processing. I hope that
SWIG will make it easy to
port extension libraries to several languages at once.
Ruby tries to do the right thing with ambiguous code, and
sometimes it makes the wrong decision. Ratul found that it
is sometimes counterintuitive: if(0) evaluates to true because 0
is an object of class integer, not nil and not false.
Some object that there are too many ways to do the same
thing in Ruby. I never understood this complaint.
5 Success
Ruby has taken over the UW networking research group. After
writing some involved Scriptroute scripts, Ratul Mahajan, a
capable Perl hacker, now uses Ruby for Scriptroute-free work
with Maya Rodrig, who is still borrowing my
pick-axe book. Tom
Anderson, faculty, rewrote our undergraduate course
programming assignment software in Ruby, and has described
the process as ``joyful.'' He gives undergraduates a week to
learn Ruby. Graduate students Gang Zhao and Stavan Parikh
use Ruby too.
Barath Raghavan, a graduate student at UCSD, made a point at
a workshop to thank me for introducing him to Ruby via
Scriptroute.
If you really don't like Ruby, or have a lot of code in
another language to integrate with, the interface offered by
the Scriptroute daemon is intentionally very simple and easy
to replicate. Just search for calls to fprintf in
ruby_classes.c. And of course, you don't need to learn Ruby
to use Scriptroute, just to develop for it.
This document was translated from LATEX by
HEVEA.