From: Luigi Ballabio Date: 2002-01-16T19:52:58+09:00 Subject: Re: Swig Ruby documentation mods. At 07:00 PM 1/16/02 +0900, Hugh Sasse Staff Elec Eng wrote: > > Have you thought about sending to the swig project? > >Of course. No feedback so far from swig-dev on this. Well, Dave's feedback is here, I think: http://mailman.cs.uchicago.edu/pipermail/swig-dev/2002-January/016502.html However, it might be that he sent the mail to the list and not to you personally, so that you might have missed it. As for you questions: >* if you don't use -module in the swig -ruby command then swig > complains. This is probably always correct, but needs mentioning. Are you sure you are including the %module directive in your .i file? I'm not using -module and SWIG doesn't complain. >* It is not clear what should be done about integrating several C > functions into the same module. Does this depend on one writing a .i > file to connect them together? How? What I usually do is: - somefuncs.i contains the declarations for some functions - someotherfuncs.i contains the declarations for some other functions - I write a file mymodule.i containing: %module MyModule %include somefuncs.i %include someotherfuncs.i - by running SWIG on the latter file, all functions from the two original ones will be exported in MyModule. >* Can you make swig roll the C functions into methods of a class, or > must one do this via including a module. If the functions all refer to a struct, you can do it in the usual SWIG way (documented for other languages, so you can probably save yourself some work by referring to the relevant places): let's say you have a struct SomeStruct and a bunch of functions: struct SomeStruct { // whatever }; void f(SomeStruct*); int g(SomeStruct*); double h(SomeStruct*, int i); You can write in the interface file: %module MyModule struct SomeStruct { // whatever }; %addmethods SomeStruct { void f() { f(self); } int g() { return g(self); } double h(int i) { return h(self,i); } } After creating the extension you'll be able to use the struct as: s = MyModule::SomeStruct.new s.f() i = f.g() x = f.h(2) If the functions are unrelated, You can declare an empty struct and do the same thing, but you'll end up with instance methods, not static methods. If the latter is what you want, you might be better off doing it on the Ruby side. Finally, if you are not forced to use C only and can use C++, you can create a class whose methods proxy the C functions and wrap such class with SWIG. >* Can extconf.rb be made to make other preliminary stuff, that is not > part of the external interface of the class? Eg, can one make it run > swig if necessary? The roughest way would be to make a system call before creating the makefile, i.e., write extconf.rb as: require 'mkmf' system('swig -ruby MyModule.i') create_makefile('MyModule') If you want to get fancy, you can compare the modification times of MyModule.i and mymodule_wrap.cpp and run swig only when needed, as in: require 'mkmf' require 'ftools' system('swig -ruby MyModule.i') unless File.exists? "mymodule_wrap.cpp" and File.mtime("mymodule_wrap.cpp") > File.mtime("MyModule.i") create_makefile('MyModule') I didn't actually run the above code, so it might need corrections. However, you get the drift. Hope this helped, Luigi