Empower Your Ruby With Haskell And Hubris
Embedding C in Ruby or Rails applications is a way to fix performance bottle necks. RubyInline made this easy for C. The Ruby community is also tightly linked with various functional programming communities: Erlang, Caml, and Haskell. Bridges already exist for Erlang with Erlectricity, and for Objective Caml with rocaml. Apache's Thrift is another way to let Ruby communicate with other languages via RPC and a serialization format.
Mark Wotton wrote Hubris, a bridge which makes it possible to call Haskell code from Ruby. You'll need to install ghc which comes up with Haskell platform in order to be able to compile jhc (John's Haskell Compiler). You should notice that jhc will only work under Linux or Mac.
Once you have the requirements, you'll firstly write an Haskell file with some extra ccall exports where you define your function, ie Test.hs:
fibonacci :: Int -> Int fibonacci n = fibs !! n where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
You'll then need to call
jhc_builder.sh Test.hs in order to create the dynamic library (
After this you can write your Ruby code and take advantage of the exported function by loading the dynamic library with Ruby/DL:
require 'dl/import' module HaskyPants extend DL::Importable dlload "./libdynhs.so" extern "int fibonacci_hs(int)" end puts HaskyPants.fibonacci_hs(12)
Mark will give a presentation about Hubris at next rorosyd in september. While still embryonic, the project will surely attract some early Haskell adopters.
Gilad Bracha Aug 27, 2014