From: David Alan Black Date: 2001-09-09T05:22:59+09:00 Subject: [ruby-talk:21014] Re: length of Ruby array from C? Hello -- On Sun, 9 Sep 2001, Neil Conway wrote: > Hi all, > > I've been experimenting with writing some Ruby extensions in C. In order > to find the length of a Ruby array, I needed to do this: > > /* -------- */ > VALUE ary; > int ary_length; > /* ... assign a Ruby array to "ary" ... */ > ary_length = RARRAY(ary)->len; > /* -------- */ > > There are 2 problems with this, IMHO: > > 1) undocumented: I had to look through the source to find this It's described in README.EXT, section 1.3: [...] For example, `RSTRING(size)->len' is the way to get the size of the Ruby String object. The allocated region can be accessed by `RSTRING(str)->ptr'. For arrays, `RARRAY(ary)->len' and `RARRAY(ary)->ptr' respectively. > 2) clumsy: if this is the only way to get the length, could we perhaps > have some syntactic sugar? For instance, a function (or macro) > rb_ary_length(VALUE), which returns the length of the specified array as > a C integer. Since this is a relatively common operation, I think it > would be worthwhile to make it a bit easier for extension writers. I think that would be sort of unwieldy, since you'd have to have rb_ary_xxxx(VALUE), rb_string_xxxx(VALUE), etc., to replace the current way of doing it by accessing C structure members. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav