From: "Asbjørn Reglund Thorsen" Date: 2005-01-28T18:22:28+09:00 Subject: Re: Extending Ruby ---259931071-1677823643-1106904111=:16946 Content-Type: MULTIPART/MIXED; BOUNDARY="-259931071-1677823643-1106904111=:16946" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---259931071-1677823643-1106904111=:16946 Content-Type: TEXT/PLAIN; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Thank you all for the response. :-) I think comp.lang.ruby is a great newsgroup with friendly and=20 highly competent people. I really enjoy reading the postings of my=20 interesst, and that`s a lot. I spend nearly an hour reading this newsgroup= =20 each day, and have allready posted at least 3 mail to it myselfe. I am missing a site for n00bs to program extending Ruby though, does there= =20 exist any ? I have read README.EXT and I have bought the wonderful=20 Pickaxe2. What about "extending" the Ruby exam site=20 (http://www.rubygarden.org/ruby?RubyExam) with an "extending Ruby" part ? On Fri, 28 Jan 2005, Charles Mills wrote: > Tim Sutherland wrote: >> In article , Lyle Johnson > wrote: >>> On Fri, 28 Jan 2005 01:56:24 +0900, Asbj=F8rn Reglund Thorsen >>> wrote: >>> > (...) >> As an alternative to rb_ary_store(), you can access the buffer > underlying >> the Array directly: >> >> $result =3D rb_ary_new2(size); >> for (int i =3D 0; i < size; ++i) >> =09RARRAY($result)->ptr[i] =3D rb_float_new((double) p(i+1))); >> >> The latter is faster since it doesn't have to check things like >> - 'Do we need to increase the buffer size' >> - 'Is the buffer shared with another Array' >> - 'Is the Array frozen' >> ...etc. > > There is a gotcha to this though. > rb_ary_new2(size) returns an array with length=3D=3D0 and capacity=3D=3Ds= ize. > The example you give above never increases length. > You could fix this with > RARRAY($result)->len =3D size; > However, this is also a gotcha. If you put it before the loop then the > array is filled with a bunch of garabage references which will crash > Ruby if the GC is run when rb_float_new() is called. If you put it > after he loop then all those floats you create could be destroyed by > the GC, causing Ruby to crash later. > rb_ary_store() is the simplest and safest solution. You could also do: > > $result =3D rb_ary_new2(size); > for ( ; RARRAY($result)->len < size; RARRAY($result)->len++) { > int i =3D RARRAY($result)->len; > RARRAY($result)->ptr[i] =3D rb_float_new((double) p(i+1))); > } > > > -Charlie > > > ---259931071-1677823643-1106904111=:16946-- ---259931071-1677823643-1106904111=:16946--