From: Charles Oliver Nutter Date: 2008-12-03T23:56:51+09:00 Subject: Re: [ANN] FFI 0.2.0 marc wrote: > Wayne Meissner said... >> Greetings Rubyists. >> >> The JRuby team is proud to announce the release of 0.2.0 of FFI for >> Ruby. This release is compatible with the FFI implementation released >> in JRuby 1.1.6 >> >> Get it via 'gem install ffi' or download the source and/or gem files >> from the project page at http://kenai.com/projects/ruby-ffi > > Just a suggestion, but I think it would be worth adding a note about > what FFI is. Here's the text of my 0.1.1 announcement, which ought to clear things up :) Perhaps the main paragraph should be included in future release announcements too? --- The JRuby team is proud to announce the release of FFI for Ruby 1.8.6/7 and 1.9! FFI (gem install ffi) is a library for programmatically loading dynamic libraries, binding functions within them, and calling those functions from Ruby code. Here's a quick sample of binding and calling the getpid C library function: require 'ffi' module GetPid extend FFI::Library attach_function :getpid, [], :uint end puts GetPid.getpid Here's another, calling qsort and passing a Ruby block as a C callback: require 'ffi' module LibC extend FFI::Library callback :qsort_cmp, [ :pointer, :pointer ], :int attach_function :qsort, [ :pointer, :int, :int, :qsort_cmp ], :int end p = MemoryPointer.new(:int, 2) p.put_array_of_int32(0, [ 2, 1 ]) puts "Before qsort #{p.get_array_of_int32(0, 2).join(', ')}" LibC.qsort(p, 2, 4) do |p1, p2| i1 = p1.get_int32(0) i2 = p2.get_int32(0) i1 < i2 ? -1 : i1 > i2 ? 1 : 0 end puts "After qsort #{p.get_array_of_int32(0, 2).join(', ')}" I posted a blog entry with a longer description of the library, additional examples, and links to some other documentation and posts. Docs are a little slim at this point, so feel free to experiment and update the JRuby wiki page: http://blog.headius.com/2008/10/ffi-for-ruby-now-available.html I'm sure docs from here will filter back into the library and out into the general cosmos. Finally, there's no need to write a C extension to call C libraries, and the same FFI code will work in Ruby 1.8.6/7, Ruby 1.9, JRuby 1.1.4+, and Rubinius (though Rubinius has no callback support yet). Don't be an extension stooge! Use FFI!