I'm pleased to announce a small helper library I've created, Nice-FFI. It sits on top of Ruby-FFI and makes certain things easier and more convenient. Nice-FFI started out as helper classes I made for Ruby-SDL-FFI (which is coming soon), but I decided to generalize it and make it its own library so that other people might get some benefit from it.
Even though it's still young, Nice-FFI already has some useful features:
- With NiceFFI::Library, it's easy to find libraries on all platforms. You can use e.g.
ffi_lib) to look in platform-specific places. E.g. on Linux, it would first look for “/usr/local/lib/libSDL.so”, while on Windows it would look for “C:\windows\sys32\SDL.dll”. Linux (and BSD), Mac, and Windows each have a good selection of default search paths already, and it's easy to add, remove, or change them using the NiceFFI::PathSet class.
- With NiceFFI::Struct, struct classes automatically get accessor methods for their members, just by calling layout. E.g.
layout :x, :int, :y, :intgives you
#y=for free. You can control which members get accessors using the
- If a struct member or function return value is a pointer to a struct, you can use
NiceFFI::TypedPointer( MyStructClass )instead of
:pointerto get auto-wrapping. So, instead of returning a Pointer, a function would return a MyStructClass instance that holds the pointer.
- New struct instances can be easily created “from scratch”, e.g.
MyStructClass.new( :x => 1, :y => 2 ). You can initialize from a Hash, Array, another instance, or a pointer as usual. Struct attributes can also be dumped to a Hash or Array. And, structs have a nice
#to_smethod to show you the values of its members, for debugging.
Nice-FFI is still young, though. So while I'd love for people to play around with it and give me feedback (and patches!), it's probably not quite ready for prime time yet. It's still under development, so the API may change suddenly. But if you are willing to tweak your code from time to time, I think you'll find Nice-FFI to be very helpful. (Otherwise, just wait until 1.0, when the API stabilizes.)
You can get Nice-FFI:
- Via RubyGems:
gem install nice-ffi
- Via Github: http://github.com/jacius/nice-ffi/
- As a tarball or zip.
Please send bug reports and feature requests to the issue tracker on Github.