~ubuntu-branches/ubuntu/warty/swig1.3/warty

« back to all changes in this revision

Viewing changes to Examples/ruby/simple/index.html

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Landschoff
  • Date: 2002-03-29 01:56:07 UTC
  • Revision ID: james.westby@ubuntu.com-20020329015607-c0wt03xu8oy9ioj7
Tags: upstream-1.3.11
ImportĀ upstreamĀ versionĀ 1.3.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
<head>
 
3
<title>SWIG:Examples:ruby:simple</title>
 
4
</head>
 
5
 
 
6
<body bgcolor="#ffffff">
 
7
 
 
8
 
 
9
<tt>SWIG/Examples/ruby/simple/</tt>
 
10
<hr>
 
11
 
 
12
<H2>Simple Ruby Example</H2>
 
13
 
 
14
<tt>$Header: /cvs/projects/SWIG/Examples/ruby/simple/index.html,v 1.1 2000/07/05 18:58:47 ttn Exp $</tt><br>
 
15
 
 
16
<p>
 
17
This example illustrates how you can hook Ruby to a very simple C program containing
 
18
a function and a global variable.
 
19
 
 
20
<h2>The C Code</h2>
 
21
 
 
22
Suppose you have the following C code:
 
23
 
 
24
<blockquote>
 
25
<pre>
 
26
/* File : example.c */
 
27
 
 
28
/* A global variable */
 
29
double Foo = 3.0;
 
30
 
 
31
/* Compute the greatest common divisor of positive integers */
 
32
int gcd(int x, int y) {
 
33
  int g;
 
34
  g = y;
 
35
  while (x > 0) {
 
36
    g = x;
 
37
    x = y % x;
 
38
    y = g;
 
39
  }
 
40
  return g;
 
41
}
 
42
</pre>
 
43
</blockquote>
 
44
 
 
45
<h2>The SWIG interface</h2>
 
46
 
 
47
Here is a simple SWIG interface file:
 
48
 
 
49
<blockquote>
 
50
<pre>
 
51
/* File: example.i */
 
52
%module example
 
53
 
 
54
extern int gcd(int x, int y);
 
55
extern double Foo;
 
56
</pre>
 
57
</blockquote>
 
58
 
 
59
<h2>Compilation</h2>
 
60
 
 
61
<ol>
 
62
<li><tt>swig -ruby <a href="example.i">example.i</a></tt>
 
63
<p>
 
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>. 
 
66
</ol>
 
67
 
 
68
<h2>Using the extension</h2>
 
69
 
 
70
Click <a href="run.rb">here</a> to see a script that calls our C functions from Ruby.
 
71
 
 
72
<h2>Key points</h2>
 
73
 
 
74
<ul>
 
75
<li>Use the <tt>require</tt> function to load your extension library from Ruby. For example:
 
76
<blockquote>
 
77
<pre>
 
78
require 'example'
 
79
</pre>
 
80
</blockquote>
 
81
 
 
82
<li>C functions work just like Ruby functions. For example:
 
83
<blockquote>
 
84
<pre>
 
85
g = Example.gcd(42,105)
 
86
</pre>
 
87
</blockquote>
 
88
 
 
89
<li>C global variables are accessed through module method. For example:
 
90
<blockquote>
 
91
<pre>
 
92
a = Example.Foo
 
93
</pre>
 
94
</blockquote>
 
95
</ul>
 
96
 
 
97
<hr>
 
98
</body>
 
99
</html>