Class: Utils::IRB::IRBServer
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.
Instance Attribute Summary collapse
-
#snippet ⇒ Object
readonly
The snippet reader method provides access to the snippet instance variable.
-
#url ⇒ String
readonly
The url reader method provides access to the URL instance variable.
Instance Method Summary collapse
-
#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.
-
#eval_snippet(code) ⇒ String
The eval_snippet method sends a code snippet to the IRB server for evaluation and returns the result.
-
#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.
-
#initialize(url:, log_out: nil) ⇒ IRBServer
constructor
The initialize method sets up a new IRBServer instance with the specified URL.
-
#start ⇒ Utils::IRB::IRBServer
The start method initializes and begins operation of the IRB server.
-
#stop_server ⇒ nil
The stop_server method sends a stop command to the IRB server.
-
#store_snippet(code) ⇒ Utils::IRB::IRBServer
The store_snippet method transmits a code snippet to the client for storage.
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.
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
#snippet ⇒ Object (readonly)
The snippet reader method provides access to the snippet instance variable.
40 41 42 |
# File 'lib/utils/irb/irb_server.rb', line 40 def snippet @snippet end |
#url ⇒ String (readonly)
The url reader method provides access to the URL 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.
144 145 146 147 148 149 |
# File 'lib/utils/irb/irb_server.rb', line 144 def eval_lines(lines, prefix = ' # => ') = build_client.transmit_with_response( { action: 'eval_lines', prefix:, lines: } ) .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.
126 127 128 129 130 131 |
# File 'lib/utils/irb/irb_server.rb', line 126 def eval_snippet(code) = build_client.transmit_with_response( { action: 'eval', snippet: code } ) .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.
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 |
#start ⇒ Utils::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.
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 || case .action when 'store' @snippet = .snippet @logger.info "Stored #{message.to_json}." when 'execute' time_eval { eval(.snippet) } @logger.info "Execution of #{message.to_json} took %.2fs" % @eval_duration when 'eval' result = time_eval { eval(.snippet) } @logger.info "Evaluation of #{message.to_json} took %.2fs" % @eval_duration .respond(result: result.to_s, type: .action) when 'eval_lines' b = binding result = .lines.map do |line| l = line.chomp r = b.eval(l) [ l, .prefix, r ].join end.join(?\n) .respond(result: result, type: .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_server ⇒ nil
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.
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.
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 |