From: Ken Bloom Date: 2008-11-04T23:38:43+09:00 Subject: Re: [ANN] FFI 0.1.1 (Foreign Function Interface) for Ruby 1.8.6/7 and 1.9 On Fri, 31 Oct 2008 18:16:39 -0500, Charles Oliver Nutter wrote: > 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://wiki.jruby.org/wiki/Calling_C_from_JRuby > > 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! > > - Charlie Based on all of the examples I've seen, there's something that's still not clear to me. How does ruby-ffi know which library to load to find the specified function? --Ken -- Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/