The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
rocaml-0.6.0: fast, easy Ruby extensions in Objective Caml

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
rocaml-0.6.0: fast, easy Ruby extensions in Objective Caml Posted: Oct 17, 2007 9:45 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: rocaml-0.6.0: fast, easy Ruby extensions in Objective Caml
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

rocaml: fast, easy Ruby extensions in Objective Caml allows you to write Ruby extensions in Objective Caml.

Developing Ruby extensions with rocaml is easier and more convenient than writing a plain old C extension because rocaml performs Ruby<->OCaml 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 variants of tuples of ...). Moreover, exceptions raised in the OCaml code are captured by the generated extension and raised inside Ruby.

Making an extension with rocaml involves two steps:

  • implementing the desired functionality in Objective Caml, and registering the functions to be exported (using Callback.register : string -> 'a -> unit or the included camlp4 extension)
  • creating the extconf.rb file (just modify the sample extconf.rb distributed with rocaml) defining the interface of your Objective Caml code.

At no point is there any need to write a single line of C code when using rocaml.

The mandatory trivial example

This example doesn't do justice to the usefulness of rocaml because the extension is beyond trivial and you could as well have written it in C using RubyInline. The advantages of rocaml (and of Objective Caml) usually become visible when the extension takes more than two lines (take a look at the 3-line Marshal replacement that is 3 times faster than Ruby's, though...). Here follows a minimal example however, merely to show how easily rocaml extensions can be made.

Here's the OCaml code placed in fib.ml:

let rec fib n = if n < 2 then 1 else fib (n-1) + fib (n-2)
export fib

Here's the interface declaration in your extconf.rb:

Interface.generate("fib") do
  def_module("Fib") do
    fun "fib", INT => INT
  end
end

That's it. The extension can be built like any run-of-the-mill C extension with

 ruby extconf.rb
 make

The resulting Ruby extension that can be used as usual:

 require 'fib'
 p Fib.fib 10

Read more...

Read: rocaml-0.6.0: fast, easy Ruby extensions in Objective Caml

Topic: Rails: Rise, Fall, and Potential Rebirth of the Presenter Pattern Previous Topic   Next Topic Topic: I got yer lambda right here, pal!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use