I'm not sure how to write this code more cleanly in ruby? The only thing that differs is the iterator.
if items.respond_to?(:find_each)
items.find_each do |item|
output_item(csv, item)
end
else
items.each do |item|
output_item(csv, item)
end
end
You can use Object#send to call method dynamically:
method = items.respond_to?(:find_each) ? :find_each : :each
items.send(method) do |item|
output_item(csv, item)
end
I suppose you could do:
method = items.respond_to?(:find_each) ? :find_each : :each
items.send(method) { |item| output_item(csv, item) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With