time_tracker.rb 516 B

12345678910111213141516171819202122
  1. class TimeTracker
  2. attr_accessor :elapsed_time, :result
  3. def self.track
  4. start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  5. result = yield
  6. new(Process.clock_gettime(Process::CLOCK_MONOTONIC) - start, result)
  7. end
  8. def initialize(elapsed_time, result)
  9. @elapsed_time = elapsed_time
  10. @result = result
  11. end
  12. def method_missing(method_sym, *arguments, &block)
  13. if @result.respond_to?(method_sym)
  14. @result.send(method_sym, *arguments, &block)
  15. else
  16. super
  17. end
  18. end
  19. end