Array.assoc

If I want to find the source to the Class Array … specifically
Array.assoc … where would I look?

On Nov 14, 9:31 pm, Ralph S. [email protected] 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. (Do you have the source code, or do you need help
finding where that is?)

On Nov 14, 9:48 pm, Phrogz [email protected] wrote:

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.

Bah, of course I meant rb_ary_assoc. Looking at your pasted text while
typing is bad. :stuck_out_tongue:

As penance, here is the source code for that function:

VALUE
rb_ary_assoc(VALUE ary, VALUE key)
{
long i;
VALUE v;

for (i = 0; i < RARRAY_LEN(ary); ++i) {

v = rb_check_array_type(RARRAY_PTR(ary)[i]);
if (!NIL_P(v) && RARRAY_LEN(v) > 0 &&
rb_equal(RARRAY_PTR(v)[0], key))
return v;
}
return Qnil;
}

On Nov 15, 2009, at 01:19 , Ralph S. wrote:

© 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?

Well, first, write it in pure ruby. Ruby classes are open and there
isn’t a need to write it in C yet.

If and when you decide it really should be added, the proper place to
propose it is [email protected].

Phrogz,

P> On Nov 14, 9:48 pm, Phrogz [email protected] wrote:

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. :stuck_out_tongue:

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 RDoc Documentation 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?