Creating new arrays from existing arrays in Ruby
<p>I have the following 2 arrays:</p>

<pre><code>fields = ["name", "team", "number", "name", "team", "number", "name", "team", "number"]
values = ["Patrick Ewing", "New York Knicks", 33, "Rik Smits", "Indiana Pacers", 45, "Bill Russell", "Boston Celtics", 6]
</code></pre>

<p>The fields will always be only name, team and number, but the number of values (i.e., number of players) will vary each time.</p>

<p>I want to create new arrays for each field type, so that I will get the following:</p>

<pre><code>names = ["Patrick Ewing", "Rik Smits", "Bill Russell"]
teams = ["New York Knicks", "Indiana Pacers", "Boston Celtics"]
numbers = [33,45,6]
</code></pre>

<p>What's a good way to do this? I have tried the following, but want to know if there are other solutions that would perform better with larger arrays (up to 300). Or would the difference be negligible?</p>

<pre><code>names = values.values_at(*(fields.each_index.select{ |i| fields[i] == "name"}))
teams = values.values_at(*(fields.each_index.select{ |i| fields[i] == "team"}))
numbers = values.values_at(*(fields.each_index.select{ |i| fields[i] == "number"}))
</code></pre>