`SWIG/Examples/ruby/simple/`

## Simple Ruby Example

`$Header: /cvsroot/CableSwig/CableSwig/SWIG/Examples/ruby/simple/index.html,v 1.1.1.1 2003/04/09 17:34:09 hoffman Exp $`

This example illustrates how you can hook Ruby to a very simple C program containing a function and a global variable.

## The C Code

Suppose you have the following C code:

>     /* File : example.c */

>     /* A global variable */
>     double Foo = 3.0;

>     /* Compute the greatest common divisor of positive integers */
>     int gcd(int x, int y) {
>       int g;
>       g = y;
>       while (x > 0) {
>         g = x;
>         x = y % x;
>         y = g;
>       }
>       return g;
>     }

## The SWIG interface

Here is a simple SWIG interface file:

>     /* File: example.i */
>     %module example

>     extern int gcd(int x, int y);
>     extern double Foo;

## Compilation

1. `swig -ruby example.i`

2. Compile `example_wrap.c` and `example.c` to create the extension `example.so`.

## Using the extension

Click here to see a script that calls our C functions from Ruby.

## Key points

- Use the `require` function to load your extension library from Ruby. For example:

  >     require 'example'

- C functions work just like Ruby functions. For example:

  >     g = Example.gcd(42,105)

- C global variables are accessed through module method. For example:

  >     a = Example.Foo