eigenclass logo
MAIN  Index  Search  Changes  PageRank  Login

rocaml: fast, easy Ruby extensions in Objective Caml


rocaml allows you to write Ruby extensions (that is, extensions meant to be used with the reference implementation of Ruby, developed by matz and others) in Objective Caml.

Developing Ruby extensions with rocaml is easier and more convenient than writing a plain old C extension since (in addition to coding in Objective Caml instead of C):

  • rocaml handles type conversions for a wide range of types, including abstract types and arrays, tuples, variants and records of values of any supported type (e.g. arrays of arrays of objects)
  • exceptions raised in the OCaml code are automatically intercepted and re-raised by the extension in Ruby
  • the boilerplate code that registers the Ruby methods, as well as all the code to wrap the calls to Objective Caml functions, is generated automatically. At no point is there any need to write a single line of C code when using rocaml.


The online repository holding the latest sources is at http://eigenclass.org/repos/rocaml/head/

The latest tarball is rocaml-0.6.1

Creating an extension

Making an extension with rocaml involves two steps:

  • implementing the desired functionality in Objective Caml, and registering the functions to be exported, using either Callback.register (of type string -> 'a -> unit) or the supplied camlp4 extension, which is automatically generated and built by rocaml and allows you to do
 (* definition of foo, bar, baz *)
 namespace "Whatever"
 export foo, bar, baz
 export (function x y -> foo y x ) aliased "barbaz"
  • creating the extconf.rb file (just modify the sample extconf.rb distributed with rocaml) defining the interface of your Objective Caml code.


  • Ruby 1.8 or 1.9
  • the C toolchain used to build normal Ruby extensions
  • ocaml -- rocaml has been tested with ocaml 3.09.2 and 3.10.0, but it should work with any version compatible with your Objective Caml code (as long as a few functions like Callback.register and the command-line interface are compatible)

Making an extension: first steps

  • copy rocaml.rb and rocaml_extconf.rb to the source directory.
  • add the appropriate Callback.register calls and/or export statements to your ML code in order to export the functions to be made available to Ruby.
  • create an extconf.rb file based on the sample one distributed with rocaml. The interface must match the functions exported in the previous step.

Note that rocaml only does as much as a normal extconf.rb: if you want to provide a way to install your extension, either use setup.rb or RubyGems. Normally, all you have to do is pretend that it is a plain old C extension and package it as usual, by placing the .ml(i) sources, extconf.rb, rocaml.rb and rocaml_extconf.rb under a directory determined by your packaging system.

Building a rocaml-based extension

$ ruby extconf.rb
$ make


rocaml Copyright (c) 2007 Mauricio Fernandez <mfp@acm.org>

Use and redistribution subject to the same conditions as Ruby. See the LICENSE file included in rocaml's distribution for more information.

(comments might be moved to other nodes if they are specific to a rocaml version)

Yes. - Kiggity (2007-10-18 (Thr) 07:59:57)

Seriously, rocaml is something I've dreamed about the existence of for years.

You've literally made my dreams come true. My geeky, pathetic dreams.

mfp 2007-10-18 (Thr) 17:21:26

Glad you like it, hope it will work well on your platform (so far, it's been tested on sparc-solaris2.10 and i686-linux).

I don't think geeky dreams are any more pathetic than any other. People wish for stupid and shallow things.


on ubuntu - albert (2007-10-22 (Mon) 18:35:03)

Apart from the (for me obvious libs) I had to install the package ocaml-native-compilers. It is super cool btw. Now, to learn ocaml.. :)

mfp 2007-10-22 (Mon) 19:47:35

I might write a bit about learning OCaml coming from Ruby. Some things are much closer than you would think, such as duck-typing and structural typing...


how to catch an absol - austin bailey (2007-10-29 (Mon) 16:47:03)

go to route 120 and walk in the grass for a little while. be sure to bring a lot of ultra balls with you


Use this form to create a new top-level comment; for direct replies to existing comments, use the text entries you'll find below.


Last modified:2007/10/17 09:06:45
Keyword(s):[ruby] [ocaml] [rocaml] [extension]
References:[rocaml-0.6.1: fast, easy Ruby extensions in Objective Caml]