Class: Tidewave::Tools::ProjectEval

Inherits:
Base
  • Object
show all
Defined in:
lib/tidewave/tools/project_eval.rb

Instance Method Summary collapse

Methods inherited from Base

file_system_tool, file_system_tool?

Instance Method Details

#call(code:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tidewave/tools/project_eval.rb', line 20

def call(code:)
  original_stdout = $stdout
  original_stderr = $stderr

  stdout_capture = StringIO.new
  stderr_capture = StringIO.new
  $stdout = stdout_capture
  $stderr = stderr_capture

  begin
    result = eval(code)
    stdout = stdout_capture.string
    stderr = stderr_capture.string

    if stdout.empty? && stderr.empty?
      result
    else
      {
        stdout: stdout,
        stderr: stderr,
        result: result
      }
    end
  ensure
    $stdout = original_stdout
    $stderr = original_stderr
  end
end