On Apr 4, 2005 10:35 AM, Joe Van Dyk <joevandyk / gmail.com> wrote:
> On Apr 4, 2005 8:34 AM, Charles Mills <cmills / freeshell.org> wrote:
> >
> > > Are there any general rules of thumb that you use when writing
> > extensions?
> >
> > http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/47700
> >
> > You shouldn't be able to cause your extension to seg fault from Ruby.
> > That may sound like a joke, but I just mean using StringValue,
> > NUM2LONG, etc before you starting using Ruby objects in C.
> >
> > >
> > > My first problem was trying to figure out what should remain Ruby
> > code
> > > and what should be moved out to C.  For example, if I opened the file
> > > that contained binary data in Ruby, how could I "get" to that data
> > > using a FILE pointer in C?  Or should I just pass the filename to the
> > > C extension?
> > >
> > You can do either.  IMO it depends on the layout of the file.  If your
> > reading line by line you can do this easily using the rb_io_* functions
> > found in intern.h.  If your going to be moving the file position
> > around, using fgetc/ungetc, or reading into C structs it may be easier
> > to use the stdio functions directly.
> >
> > -Charlie
> 
> So, if I have a IO object in Ruby, how could I use C's stdio functions
> on it?  And how could I have figured it out without asking the mailing
> list?  :-)  Cuz I'm sure I'll have lots more questions.
> 
> Thanks,
> Joe


And to expand the question...

I'm using the GD library to create the image.  I'm creating the image
by reading binary data from a file and looping through some
predetermined width and height and setting each pixel of the picture
based on the data from the file.

Which parts of this should go in the C extension and which parts
should stay Ruby?  I'm really scratching my head over this one.

Thanks,
Joe