From: Ralph Shnelvar Date: 2009-11-15T18:19:51+09:00 Subject: Re: Array.assoc Phrogz, P> On Nov 14, 9:48�pm, Phrogz wrote: >> On Nov 14, 9:31�pm, Ralph Shnelvar wrote: >> >> > If I want to find the source to the Class Array ... specifically >> > Array.assoc ... where would I look? >> >> Slim2:~ phrogz$ cd /usr/local/src/ruby-1.9.1-p243/ >> Slim2:ruby-1.9.1-p243 phrogz$ grep -E "define_method.+assoc" *.c >> array.c: � �rb_define_method(rb_cArray, "assoc", rb_ary_assoc, 1); >> array.c: � �rb_define_method(rb_cArray, "rassoc", rb_ary_rassoc, 1); >> hash.c: � �rb_define_method(rb_cHash, "assoc", rb_hash_assoc, 1); >> hash.c: � �rb_define_method(rb_cHash, "rassoc", rb_hash_rassoc, 1); >> >> So, look for the function named rb_cArray in the array.c file in the >> ruby source code. P> Bah, of course I meant rb_ary_assoc. Looking at your pasted text while P> typing is bad. :p P> As penance, here is the source code for that function: P> VALUE P> rb_ary_assoc(VALUE ary, VALUE key) P> { P> long i; P> VALUE v; P> for (i = 0; i < RARRAY_LEN(ary); ++i) { P> v = rb_check_array_type(RARRAY_PTR(ary)[i]); P> if (!NIL_P(v) && RARRAY_LEN(v) > 0 && P> rb_equal(RARRAY_PTR(v)[0], key)) P> return v; P> } P> return Qnil; P> } Thank you very much. (A) Is there a place online where I could have found this? On my Rails-installed system I have the source to ruby-1.8.6-p111 ... and the source is slightly different. ... Never mind ... I just found it at http://ruby-doc.org/ruby-1.9/index.html by clicking on the title. Grrr. (B) Looking at this code is really fascinating to me. It tells me, for instance, that a Ruby array (at least in 1.9.1-p243) is really a C array of pointers. Nice! I was concerned that it might translate into some sort of array of array of pointers (sorta like how a FAT table is implement to allocate files). (C) Assume that ary is sorted by RARRAY_PTR(v)[0] so that one could do a binary search instead of a linear search. What user group would I talk to to talk about adding arb_ary_sortedassoc?