Hmm..can’t yet read this yet..but does Ruby Array#assoc use linear search?
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;
}
Personally, I find the Rubinius source code much easier to read than the YARV source code. (Actually, I find all other Ruby implementations’ source code easier to read than YARV or MRI.)
This is the implementation of
Array#assocfrom Rubinius:So, yes it is easy to see that it indeed does use a linear search.
But you don’t really need to look at the source code to figure that out. What else could it be? There is no structure or order that could be exploited to speed it up, unlike with a search tree or a sorted array.