From: David Alan Black Date: 2001-09-09T06:02:59+09:00 Subject: [ruby-talk:21016] Re: length of Ruby array from C? Hello -- On Sun, 9 Sep 2001, Neil Conway wrote: > > > 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. > > I still think this would be a good idea. Of the builtin structs, > 'length' only makes sense for RArray and RString. You _could_ add > functions for RRegexp, RBigNum and RStruct, but since these are > relatively rarely used (compared to RArray and RString), there would be > no need to define more than 2 functions -- rb_ary_len(VALUE) and > rb_str_len(VALUE). That's not too unwieldy, IMHO; and I find > "rb_ary_len(ary)" to be more readable than "RARRAY(ary)->len". It also > prevents extension writers from (directly) accessing the fiels of those > structures, which is generally a bad idea (as README.EXT points out). Well, *changing* them is a bad idea :-) But just reading them is such a common thing throughout the source -- not just len, but ptr, super, iv_tbl, etc. It feels (to me) like it would be inconsistent to change the interface for just one property of just two types. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav