From: mattmsykes@... (Matt M Sykes) Date: 2002-04-24T18:56:43+09:00 Subject: Re: building extensions-- new vs initialize Dennis Newbold wrote in message news:... > Hi Norman, > > Try using SWIG to build your extension. Some on this list have > had a few problems getting the resulting code to run properly, (then > again, it may). But you can look at the generated code startup logic > (entry point Init_ how to define the necessary entry points, constants, class linkages, > etc.). You can get SWIG for free from www.swig.org > > Dennis > > On Tue, 23 Apr 2002, Norman Makoto Su wrote: > > > Hi, I'm trying to build a ruby extension in C. While looking at the pickaxe CD > > player example, I noticed that they define a singleton_method called "new" and > > then a normal method called "initialize". What is the motivation for this? Is > > it so that when you call CDPlayer.new, the returned object is of type Data? > > (this can't be achieved with just initialize?) > > > > I'm confused as to when it is or not neccessary to define a "new" method in > > addition to an "initialize" method. For example, when defining class variables, > > does this have to be done in a "new" method, since the class has not been > > instantiated? > > > > One last thing, would someone mind giving me the an example of the correct way > > to make a class that includes the Singleton method and then define the > > "instance" method (using rb_require, rb_include_module, etc...) > > > > Thank you, please excuse my inexperience, > > > > -- > > Norman > > I tried SWIG last week. I think the Ruby C API is elegant and easy enough to not need SWIG. For a sample C++ class with three methods, SWIG produced 700 lines of code for the binding to Ruby --- and it didn't even work. A hand-written wrapper was 20 lines and worked flawlessly. By writing the bindings yourself you can also design a more intelligent interface to Ruby; for example you may want a "method!" or a "method?" or a "method(&block)". I can see using SWIG to create bindings for Python, since you can't make classes inside the Python C API (among other things). So to me, SWIG is sort of a kluge for languages that have a less enlightened C API :) --Matt