Ruby service objects call+run

The service objects I write in Ruby typically are called with #call, but the implementation lives in #run:

class AbstractService
  def call
    run
  end

  private

  def run
    raise "Not implemented"
  end
end

The advantage of this approach is that #call can be changed to support other use cases, like recording telemetry or logging:

class AbstractService
  def call
    key = "call_service.my_app"
    ActiveSupport::Notifications.instrument(key) do
      run
    end
  end

  # [snip]
end
Note last edited November 2023.
Backlinks: Perpetual interests.