From: Jamis Buck Date: 2004-10-29T00:30:30+09:00 Subject: Re: Ruby/DL (was Re: The real Ruby vs. Python.) Michael DeHaan wrote: > Speaking of Ruby/DL, I have a question ... > > Supposing I have a library that demands control of an event loop. > Examples could include GLUT (glutMainLoop) or SDL, but maybe it's > something else. What ways are there in Ruby/DL to call back into > Ruby code from the C library? Ruby/DL does support callbacks, but only a fixed number of them (10, by default, unless you compile the DL extension yourself and specify otherwise): module Stuff extend DL::Importable dlload "..." extern "int fn_with_cb( void* )" def my_callback( x, y, z ) do_something_with( x, y, z ) end CALLBACK = callback( "int my_callback( int x, int y, int z )" ) end Stuff.fn_with_cb( Stuff::CALLBACK ) There is an example in the libc.rb file in the sample subdirectory of the DL directory in the Ruby source distribution. - Jamis > > Is this even possible, or do I have to write Swig/XS for this? Up > until this point I haven't had a need for XS/Swig, so I've typically > gone the Ruby/DL route (in what few Ruby apps I've written), or I've > used Inline::C (awesome module, BTW) in Perl. > > Anyhow, ruby-sdl and ruby-opengl may exist, but occasionally I take > some pleasure in reinventing wheels...or at least in knowing how to. > In fact, ruby-opengl might be a good example after all. I'd really > like to stay down in C code for longer lengths of time, so this is a > case where writing one's own bindings may be useful. > > --Michael DeHaan > > On Thu, 28 Oct 2004 07:49:01 +0900, gabriele renzi > wrote: > >>Jamis Buck ha scritto: >> >> >> >>>Besides which: Ruby/DL is often touted as a great way to write extension >>>libraries. Are there any examples of libraries that have been written >>>using Ruby/DL instead of C? I've never seen any, much to my own >>>frustration when I would like a good example of Ruby/DL usage. :( >>> >> >>well I can only remember the htmltidy library, never looked at it, anyway :/ >> >> > > > . > -- Jamis Buck jgb3@email.byu.edu http://www.jamisbuck.org/jamis