My premise is that i have a large array that i’d like to iterate with
select, but
I want up to a maximum (n) number of returns. Something like
Enum#select_first_n_matches.
yes i could do my_arr.select{ blocky }.first(n). Which is a neat
one-liner, but has the performance overhead of iterating through the
entire array.
is there a method that does this already? Anyone have any slick one-line
solutions?
My working solution is:
new_arr = []; matches = 0
my_arr.each do |ele|
if condition
new_arr << ele
matches += 1
break if matches >= limit
end
end