SWIG Ruby Examples

$Header: /cvs/projects/SWIG/Examples/ruby/index.html,v 2001/12/08 23:33:29 cheetah Exp $

The following examples illustrate the use of SWIG with Ruby.

  • simple. A minimal example showing how SWIG can be used to wrap a C function, a global variable, and a constant.
  • constants. This shows how preprocessor macros and certain C declarations are turned into constants.
  • variables. An example showing how to access C global variables from Ruby.
  • value. How to pass and return structures by value.
  • class. Wrapping a simple C++ class.
  • reference. C++ references.
  • pointer. Simple pointer handling.
  • funcptr. Pointers to functions.
  • enum. Enumeration.

Compilation Issues

  • To create a Ruby extension, SWIG is run with the following options:
    % swig -ruby interface.i
  • Please see the Windows page in the main manual for information on using the examples on Windows.

  • On Unix the compilation of examples is done using the file Example/Makefile. This makefile performs a manual module compilation which is platform specific. Typically, the steps look like this (Linux):
    % swig -ruby interface.i
    % gcc -fpic -c interface_wrap.c -I/usr/local/lib/ruby/1.4/i686-linux
    % gcc -shared interface_wrap.o $(OBJS) -o 
    % ruby
    require 'interface'
  • The politically "correct" way to compile a Ruby extension is to follow the steps described README.EXT in Ruby distribution:

    1. Create a file called extconf.rb that looks like the following:
      require 'mkmf'
    2. Type the following to build the extension:
      % ruby extconf.rb
      % make


The examples have been extensively tested on the following platforms:
  • Linux
Your mileage may vary. If you experience a problem, please let us know by sending a message to