It was asked:
I want to grab the first 10 items then leave the "each" loop.
Use throw
and catch
to accomplish this, with few changes to the example:
catch(:done) do
c = 0
collected = []
items.each do |item|
collected << item
throw(:done, collected) if c == 9 # started at 0
c += 1
end
collected # if the list is less than 10 long, return what was collected
end
Simply throw
the label :done
with collected
and the catch
which is waiting for :done
will return collected
.
And to "ruby" this up a bit:
catch(:done) do
items.inject([]) do |collected, item|
throw(:done, collected) if collected.size == 10
collected << item # collected gets returned here and populates the first argument of this block
end
end
I do not know why some people refuse to use inject
and use reduce
instead (they are equivalent) when clearly the empty array given to inject([])
is being injected with item
s! Anyhow, the inject
will return collected
if there are less than 10 items.
Most answers are trying to answer what might be the intent of the question instead of what was asked and items.take(10)
does make perfect sense in that case. But I can imagine wanting to grab the first items that fit within my $100 budget. Then you can simply:
catch(:done) do
items.inject({items: [], budget: 100}) do |ledger, item|
remainder = ledger[:budget] - item.price
if remainder < 0
throw(:done, ledger)
else
ledger.tap do |this|
this[:items] << item
this[:budget] = remainder
end # tap just returns what is being tapped into, in this case, ledger
end
end
end