Array.assoc

R

Ralph Shnelvar

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

Phrogz

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?)
 
P

Phrogz

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));
if (!NIL_P(v) && RARRAY_LEN(v) > 0 &&
rb_equal(RARRAY_PTR(v)[0], key))
return v;
}
return Qnil;
}
 
R

Ralph Shnelvar

Phrogz,



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));
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?
 
R

Ryan Davis

(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 (e-mail address removed).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top