From: David Landrith Date: 2003-03-25T01:07:40+09:00 Subject: Writing a new method in C for a subclass --Apple-Mail-2--652104154 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Given the available documentation and having gone through the ruby source code, I understand how to write a new method in C. My question is, how do I write a "new" method for a class that extends another class. Specifically, I want to extend Array, but I am not clear on exactly how to do this. If my conception is correct, then appears to me that I must make a struct with all of the values used by my class. However, since I am extending Array, I am not sure whether I need to some how construct it so that it is uses the RBasic, len, capa, and ptr values in the Array class and then adds its own values, or if I can simply duplicate these in my extension struct. Unless all inheritance is handled by rb_define_class, something like this would seem to be needed in order to preserve the existing functionality that I want to inherit from Array. Right now, I define an 'initialize' method in C, instantiate the object (which extends Array) in Ruby, pass the already instantiated object as a parameter to another C method that returns that same object populated with the appropriate data, and push that object onto an array. This process is enclosed in a loop that can conceivably repeat hundreds of thousands of times, so speed is of the essence. It strikes me that it would be faster to instantiate the object in the C method that currently takes it as a parameter, and simply return that object--or better yet execute whole loop in the C method so that it returns the array with all of the objects already pushed onto it. Does this sound sensible? At any rate, I may have overlooked something, but I haven't found any documentation on this, and I'd have to dig much deeper into the source code to figure it out. Could someone please clarify this? Thanks, Dave ------------------------------------------------------- David King Landrith (w) 617.227.4469x213 (h) 617.696.7133 One useless man is a disgrace, two are called a law firm, and three or more become a congress -- John Adams ------------------------------------------------------- public key available upon request --Apple-Mail-2--652104154 Content-Transfer-Encoding: 7bit Content-Type: text/enriched; charset=US-ASCII Given the available documentation and having gone through the ruby source code, I understand how to write a new method in C. My question is, how do I write a "new" method for a class that extends another class. Specifically, I want to extend Array, but I am not clear on exactly how to do this. If my conception is correct, then appears to me that I must make a struct with all of the values used by my class. However, since I am extending Array, I am not sure whether I need to some how construct it so that it is uses the RBasic, len, capa, and ptr values in the Array class and then adds its own values, or if I can simply duplicate these in my extension struct. Unless all inheritance is handled by rb_define_class, something like this would seem to be needed in order to preserve the existing functionality that I want to inherit from Array. Right now, I define an 'initialize' method in C, instantiate the object (which extends Array) in Ruby, pass the already instantiated object as a parameter to another C method that returns that same object populated with the appropriate data, and push that object onto an array. This process is enclosed in a loop that can conceivably repeat hundreds of thousands of times, so speed is of the essence. It strikes me that it would be faster to instantiate the object in the C method that currently takes it as a parameter, and simply return that object--or better yet execute whole loop in the C method so that it returns the array with all of the objects already pushed onto it. Does this sound sensible? At any rate, I may have overlooked something, but I haven't found any documentation on this, and I'd have to dig much deeper into the source code to figure it out. Could someone please clarify this? Thanks, Dave Helvetica------------------------------------------------------- David King Landrith (w) 617.227.4469x213 (h) 617.696.7133 One useless man is a disgrace, two are called a law firm, and three or more become a congress -- John Adams ------------------------------------------------------- public key available upon request --Apple-Mail-2--652104154--