Class: RemoteRuby::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_ruby/runner.rb

Overview

Runner class is responsible for running a prepared Ruby code with given connection adapter, reading output and unmarshalling result and local variables values.

Instance Method Summary collapse

Constructor Details

#initialize(code:, adapter:, out_stream: $stdout, err_stream: $stderr) ⇒ Runner

Returns a new instance of Runner.



10
11
12
13
14
15
# File 'lib/remote_ruby/runner.rb', line 10

def initialize(code:, adapter:, out_stream: $stdout, err_stream: $stderr)
  @code = code
  @adapter = adapter
  @out_stream = out_stream
  @err_stream = err_stream
end

Instance Method Details

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/remote_ruby/runner.rb', line 17

def run
  locals = nil

  adapter.open(code) do |stdout, stderr|
    out_thread = read_stream(stdout, out_stream, :green)
    err_thread = read_stream(stderr, err_stream, :red)
    [out_thread, err_thread].each(&:join)
    locals = out_thread[:locals]
  end

  { result: locals[:__return_val__], locals: locals }
end