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
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)
- 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.
$ ruby extconf.rb $ make
rocaml Copyright (c) 2007 Mauricio Fernandez <email@example.com> http://eigenclass.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)
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.
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.
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.. :)
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...
go to route 120 and walk in the grass for a little while. be sure to bring a lot of ultra balls with you
- 11 http://search.live.com/results.aspx?q=rocaml&mrt=en-us&FORM=LIVSOP
- 6 http://www.ruby-forum.com/topic/128370
- 4 http://planetruby.0x42.net
- 3 http://rsdn.ru/forum/message/2697320.1.aspx
- 2 http://search.live.com/results.aspx?q=my fast ruby cheats now&FORM=SSRE2
- 2 http://www.rsdn.ru/Forum/Message.aspx?mid=2697320&only=1
- 2 http://www.ruby-forum.com/topic/128370#new
- 1 http://www.artima.com/forums/flat.jsp?forum=123&thread=217127
- 1 http://www.rsdn.ru/forum/message/2697320.1.aspx
- 1 http://www.google.com/url?sa=D&q=http://eigenclass.org:80/hiki/rocaml