class Tryouts::TestBatch
def execute_with_output_capture(container, code, path, range)
def execute_with_output_capture(container, code, path, range) # Fiber-local storage for output redirection original_stdout = $stdout original_stderr = $stderr # Create StringIO objects for capturing output captured_stdout = StringIO.new captured_stderr = StringIO.new begin # Redirect output streams using Fiber-local variables Fiber.new do $stdout = captured_stdout $stderr = captured_stderr # Execute with timing capture execution_start_ns = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) result_value = container.instance_eval(code, path, range.first + 1) execution_end_ns = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) execution_time_ns = execution_end_ns - execution_start_ns [result_value, execution_time_ns] end.resume.tap do |result_value, execution_time_ns| # Return captured content along with result return [result_value, execution_time_ns, captured_stdout.string, captured_stderr.string] end ensure # Always restore original streams $stdout = original_stdout $stderr = original_stderr end end