Extending Ruby
It
is easy to extend Ruby with new features by writing code in Ruby.
Once you start adding in low-level code written in C, however, the
possibilities are endless.
Extending Ruby with C is pretty easy. For instance, suppose we are
building a custom Internet-ready jukebox for the Sunset Diner and
Grill. It will play MP3 audio files from a hard disk or audio CDs
from a CD jukebox. We want to be able to control the jukebox hardware
from a Ruby program. The hardware vendor gave us a C header file and
a binary library to use; our job is to construct a Ruby object
that makes the appropriate C function calls.
But before we can get Ruby and C to work together, we need to see what
the Ruby world looks like from the C side.
[Much of the
information in this chapter is taken from the README.EXT
file that is included in the distribution. If you are planning on
writing a Ruby extension, you may want to refer to that file for
more details as well as the latest changes.]