3
<title>SWIG:Examples:ruby:simple</title>
6
<body bgcolor="#ffffff">
9
<tt>SWIG/Examples/ruby/simple/</tt>
12
<H2>Simple Ruby Example</H2>
14
<tt>$Header: /cvs/projects/SWIG/Examples/ruby/simple/index.html,v 1.1 2000/07/05 18:58:47 ttn Exp $</tt><br>
17
This example illustrates how you can hook Ruby to a very simple C program containing
18
a function and a global variable.
22
Suppose you have the following C code:
26
/* File : example.c */
28
/* A global variable */
31
/* Compute the greatest common divisor of positive integers */
32
int gcd(int x, int y) {
45
<h2>The SWIG interface</h2>
47
Here is a simple SWIG interface file:
54
extern int gcd(int x, int y);
62
<li><tt>swig -ruby <a href="example.i">example.i</a></tt>
64
<li>Compile <tt><a href="example_wrap.c">example_wrap.c</a></tt> and <tt><a href="example.c">example.c</a></tt>
65
to create the extension <tt>example.so</tt>.
68
<h2>Using the extension</h2>
70
Click <a href="run.rb">here</a> to see a script that calls our C functions from Ruby.
75
<li>Use the <tt>require</tt> function to load your extension library from Ruby. For example:
82
<li>C functions work just like Ruby functions. For example:
85
g = Example.gcd(42,105)
89
<li>C global variables are accessed through module method. For example: