Ruby Developer's Guide

You already know how to extend Ruby's built-in functionality by importing modules with the require statement. Although these extension modules are often written in Ruby, they can also be written in C (or C++) and then compiled into dynamically-loadable shared libraries that look like regular Ruby modules to the Ruby interpreter. Like regular Ruby modules, C extension modules can expose constants, methods, and classes to the Ruby interpreter. Several modules in the standard Ruby library (including socket, tk, and Win32API) are implemented as C extensions, and a survey through the Ruby Application Archive reveals a number of other popular Ruby extensions implemented using C/C++ code.
You might well ask why you would want to write a Ruby extension module in C/C++ instead of Ruby; after all, you've probably turned to Ruby as a more flexible and powerful alternative to traditional programming languages like C/C++. Nevertheless, there are two primary reasons for implementing an extension in C/C++:
Performance For some applications, the performance requirements for your extension module may necessitate a C/C++ implementation. For example, the standard Ruby library's Matrix and Vector classes (defined by the matrix.rb module) don't perform well for large-scale numerical computing applications. Third-party extensions such as NArray have been developed to fill this gap.
Interfaces to already-available C/C++ libraries If a library of useful C/C++ code already exists, it is generally quicker to develop an interface from Ruby to that library instead of reimplementing that...