---
# System prepended metadata

title: Ruby Array#values_at

---

# Ruby Array#values_at

``` ruby
require 'prime'
base = (1..100).to_a
target = Prime.take_while { |e| e < 100 }
def foo1(base, target)
  sum = 0
  base.each_with_index do |e, index|
    next unless index.in?(target)
    sum += e
  end
  sum
end
def foo2(base, target)
  sum = 0
  base.values_at(*target).each do |e|
    sum += e
  end
  sum
end

foo1(base, target) == foo2(base, target) # true
Benchmark.ms { 10000.times { foo1(base, target) } }
# => 1859.207000117749
Benchmark.ms { 10000.times { foo2(base, target) } }
# => 42.76800015941262
```