# Array.assoc

**URL:** <https://www.ruby-forum.com/t/array-assoc/178987>\
**Category:** Ruby\
**Created:** [November 15, 2009, 6:09am UTC](https://www.ruby-forum.com/t/array-assoc/178987 "2009-11-15T06:09:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 15, 2009, 6:09am UTC](https://www.ruby-forum.com/t/array-assoc/178987/1 "2009-11-15T06:09:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 15, 2009, 6:24am UTC](https://www.ruby-forum.com/t/array-assoc/178987/2 "2009-11-15T06:24:27Z")

</div>

On Nov 14, 9:31 pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?)

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 15, 2009, 6:24am UTC](https://www.ruby-forum.com/t/array-assoc/178987/3 "2009-11-15T06:24:27Z")

</div>

On Nov 14, 9:48 pm, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 😛

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

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 15, 2009, 12:31pm UTC](https://www.ruby-forum.com/t/array-assoc/178987/4 "2009-11-15T12:31:57Z")

</div>

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 removed\_email\_address@domain.invalid.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 15, 2009, 10:20am UTC](https://www.ruby-forum.com/t/array-assoc/178987/5 "2009-11-15T10:20:17Z")

</div>

Phrogz,

P\> On Nov 14, 9:48 pm, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 😛

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