Class: Utils::IRB::IRBServer

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/irb/irb_server.rb

Overview

A class that provides server functionality for interactive Ruby (IRB) sessions.

This class manages an IRB server instance that can receive and process code snippets for evaluation. It handles communication through Unix domain sockets, allowing external clients to send code for execution and receive the results back.

Examples:

server = Utils::IRB::IRBServer.new(url: 'unix:///tmp/irb.sock')
server.start
#
client = Utils::IRB::IRBServer.new(url: 'unix:///tmp/irb.sock')
client.store_snippet('puts "Hello World"')
result = server.eval_snippet('2 + 2')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, log_out: nil) ⇒ IRBServer

The initialize method sets up a new IRBServer instance with the specified URL.

This method configures the IRB server by initializing a logger for error reporting and storing the provided URL for server communication.

Parameters:

  • url (String)

    the URL to be used for the IRB server communication



27
28
29
30
# File 'lib/utils/irb/irb_server.rb', line 27

def initialize(url:, log_out: nil)
  @url     = url
  @log_out = log_out
end

Instance Attribute Details

#snippetObject (readonly)

The snippet reader method provides access to the snippet instance variable.

Returns:

  • (Object)

    the snippet value stored in the instance variable



40
41
42
# File 'lib/utils/irb/irb_server.rb', line 40

def snippet
  @snippet
end

#urlString (readonly)

The url reader method provides access to the URL instance variable.

Returns:

  • (String)

    the URL value stored in the instance variable



35
36
37
# File 'lib/utils/irb/irb_server.rb', line 35

def url
  @url
end

Instance Method Details

#eval_lines(lines, prefix = ' # => ') ⇒ String

The eval_lines method sends a series of code lines to the IRB server for evaluation and returns the formatted results.

This method transmits multiple lines of code to the IRB server for execution, with each line being evaluated in sequence. It includes a prefix for each result line and returns the combined output from the evaluation.

Parameters:

  • lines (Array<String>)

    the array of code lines to be evaluated

  • prefix (String) (defaults to: ' # => ')

    the prefix to be added to each result line, defaults to ‘ # => ’

Returns:

  • (String)

    the formatted results from evaluating the code lines



144
145
146
147
148
149
# File 'lib/utils/irb/irb_server.rb', line 144

def eval_lines(lines, prefix = ' # => ')
  message = build_client.transmit_with_response(
    { action: 'eval_lines', prefix:, lines: }
  )
  message.result
end

#eval_snippet(code) ⇒ String

The eval_snippet method sends a code snippet to the IRB server for evaluation and returns the result.

This method transmits the provided code snippet to the IRB server for execution, waits for the server’s response, and extracts the evaluation result from the response.

Parameters:

  • code (String)

    the code snippet to be evaluated

Returns:

  • (String)

    the result of the code snippet evaluation as a string



126
127
128
129
130
131
# File 'lib/utils/irb/irb_server.rb', line 126

def eval_snippet(code)
  message = build_client.transmit_with_response(
    { action: 'eval', snippet: code }
  )
  message.result
end

#execute_snippet(code) ⇒ Utils::IRB::IRBServer

The execute_snippet method sends a code snippet to the IRB server for execution and waits for the response.

This method transmits an execute command along with the provided code snippet to the IRB server, allowing the server to evaluate the code and return the result.

Parameters:

  • code (String)

    the code snippet to be executed

Returns:



111
112
113
114
# File 'lib/utils/irb/irb_server.rb', line 111

def execute_snippet(code)
  build_client.transmit({ action: 'execute', snippet: code })
  self
end

#startUtils::IRB::IRBServer

The start method initializes and begins operation of the IRB server.

This method sets up the server by building the underlying socket connection, logging the start event, and configuring a background receiver to handle incoming messages. It processes different message actions such as storing code snippets or evaluating code, and responds appropriately to each message type.

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/utils/irb/irb_server.rb', line 51

def start
  @server = build_server
  @logger.info "Starting #{self.class.name} server on #{@url}."
  @server.receive_in_background do |message|
    case message.action
    when 'store'
      @snippet = message.snippet
      @logger.info "Stored #{message.to_json}."
    when 'execute'
      time_eval { eval(message.snippet) }
      @logger.info "Execution of #{message.to_json} took %.2fs" % @eval_duration
    when 'eval'
      result = time_eval { eval(message.snippet) }
      @logger.info "Evaluation of #{message.to_json} took %.2fs" % @eval_duration
      message.respond(result: result.to_s, type: message.action)
    when 'eval_lines'
      b = binding
      result = message.lines.map do |line|
        l = line.chomp
        r = b.eval(l)
        [ l, message.prefix, r ].join
      end.join(?\n)
      message.respond(result: result, type: message.action)
    when 'stop'
      @logger.info "Stopping #{self.class.name} server on #{@url}."
      Thread.current.exit
    else
      @logger.warn("Message for action #{message.action.inspect} not supported.")
    end
  rescue => e
    @logger.error("#{self.class.name} caught #{e.class}: #{e} for #{message.to_json}.")
  end
  self
end

#stop_servernil

The stop_server method sends a stop command to the IRB server.

This method communicates with the IRB server to request a graceful shutdown of the server process by transmitting a stop action.

Returns:

  • (nil)

    always returns nil after sending the stop command



157
158
159
160
# File 'lib/utils/irb/irb_server.rb', line 157

def stop_server
  build_client.transmit({ action: 'stop' })
  nil
end

#store_snippet(code) ⇒ Utils::IRB::IRBServer

The store_snippet method transmits a code snippet to the client for storage.

This method prepares a transmission request containing the specified code snippet and sends it to the client using the build_client mechanism. It is designed to facilitate the storage of code snippets within the system’s communication protocol.

Parameters:

  • code (String)

    the code snippet to be stored

Returns:



96
97
98
99
# File 'lib/utils/irb/irb_server.rb', line 96

def store_snippet(code)
  build_client.transmit({ action: 'store', snippet: code })
  self
end