Ruby call next on an enumerator from an external method

152 views Asked by At

I'm writing some DSL and I'd like to have a repurposable method that yields to a block and traps any exceptions.

Example:

def trap_exception(&block)
  yield
rescue StandardError => e
  log "Error: #{e.message}"
  next
end

I'd then use it like this:

# items is an array
items.each do |item|
  trap_exception do
    # something that might raise an exception. If so, pass to the next item in array
  end
end

This is going to raise an error because next is not being called within an enumerable.

I'm looking to keep the code within the trap_exception block as clean as possible. I'll also be using this method in a number of different places so I'm trying to minimize code duplication as much as possible.

What would be the best way to accomplish this?

0

There are 0 answers