Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is order preserved in arrays when intersection is done?

Tags:

arrays

ruby

When I do an intersection of two arrays, is there any guarantee that the resulting order is based on the order of the first array?

For example, if I have

a = [1,2,3]
b = [3,2,1]

could a & b return [3,2,1], instead of [1,2,3] (which is what I'd expect)?

I can't find anything directly addressing this in either RDoc's or the Pickaxe's documentation of Array.

RubySpec has a specification that it creates an array with elements in order they are first encountered, but should I assume YARV Ruby will honour that specification?

like image 897
Andrew Grimm Avatar asked Feb 08 '13 01:02

Andrew Grimm


1 Answers

Looks like it is a guaranteed feature. They upgraded the RDoc description in revision 39415. It should be reflected sometime.

like image 185
sawa Avatar answered Nov 02 '22 22:11

sawa