After watching Peter Cooper’s Ruby Trick Shots I wanted to experiment with loading dynamic libraries in Ruby as he demoed.

Maddeningly, when I tried to require fiddle, Ruby was throwing a LoadError:

LoadError: cannot load such file -- fiddle

Digging into the RVM logs in ~/.rvm/log/ruby-1.9.3-p0, I found that when RVM compiled Ruby it failed to find the ffi.h header and subsequently did not install fiddle.

$ grep -A 2 fiddle ~/.rvm/log/ruby-1.9.3-p0/make.log 
configuring fiddle
ffi.h is missing. Please install libffi.
...

So, as usual this is a dependency problem and your solution is an apt-get away:

$ sudo apt-get install libffi5 libffi-dev

Now, I can finally run Peter’s code:

#!/usr/bin/env ruby

require 'fiddle'

libc = DL.dlopen "libc.so.6"

f = Fiddle::Function.new(libc['strlen'],
                         [Fiddle::TYPE_VOIDP],
                         Fiddle::TYPE_INT)

p f.call("foo").to_i

http://rubyreloaded.com/trickshots/
http://rubydoc.info/stdlib/fiddle/frames