Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array.assoc

1 view
Skip to first unread message

Ralph Shnelvar

unread,
Nov 14, 2009, 11:31:32 PM11/14/09
to
If I want to find the source to the Class Array ... specifically
Array.assoc ... where would I look?


Phrogz

unread,
Nov 14, 2009, 11:48:17 PM11/14/09
to
On Nov 14, 9:31 pm, Ralph Shnelvar <ral...@dos32.com> 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?)

Phrogz

unread,
Nov 14, 2009, 11:50:13 PM11/14/09
to
On Nov 14, 9:48 pm, Phrogz <phr...@mac.com> wrote:
> On Nov 14, 9:31 pm, Ralph Shnelvar <ral...@dos32.com> 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.

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

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;
}

Ralph Shnelvar

unread,
Nov 15, 2009, 4:19:51 AM11/15/09
to
Phrogz,


P> On Nov 14, 9:48�pm, Phrogz <phr...@mac.com> wrote:
>> On Nov 14, 9:31�pm, Ralph Shnelvar <ral...@dos32.com> 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?

Ryan Davis

unread,
Nov 15, 2009, 6:31:17 AM11/15/09
to

On Nov 15, 2009, at 01:19 , Ralph Shnelvar wrote:

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

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 ruby...@ruby-lang.org.


0 new messages