1+ import re
2+ from pathlib import Path
3+ from .subprocess_language import SubprocessLanguage
4+
5+
6+ class Ruby (SubprocessLanguage ):
7+ file_extension = "rb"
8+ name = "Ruby"
9+
10+ def __init__ (self ):
11+ super ().__init__ ()
12+ self .start_cmd = ["irb" ]
13+
14+ def preprocess_code (self , code ):
15+ """
16+ Add active line markers
17+ Wrap in a tryCatch for better error handling
18+ Add end of execution marker
19+ """
20+
21+ lines = code .split ("\n " )
22+ processed_lines = []
23+
24+ for i , line in enumerate (lines , 1 ):
25+ # Add active line print
26+ processed_lines .append (f'puts "##active_line{ i } ##"' )
27+ processed_lines .append (line )
28+ # Join lines to form the processed code
29+ processed_code = "\n " .join (processed_lines )
30+
31+ # Wrap in a tryCatch for error handling and add end of execution marker
32+ processed_code = f"""
33+ begin
34+ { processed_code }
35+ rescue => e
36+ puts "##execution_error##\\ n" + e.message
37+ ensure
38+ puts "##end_of_execution##\\ n"
39+ end
40+ """
41+ self .code_line_count = len (processed_code .split ("\n " ))
42+ #print(processed_code)
43+ return processed_code
44+
45+ def line_postprocessor (self , line ):
46+ # If the line count attribute is set and non-zero, decrement and skip the line
47+ if hasattr (self , "code_line_count" ) and self .code_line_count > 0 :
48+ self .code_line_count -= 1
49+ return None
50+ if "nil" in line :
51+ return None
52+ return line
53+
54+ def detect_active_line (self , line ):
55+ if "##active_line" in line :
56+ return int (line .split ("##active_line" )[1 ].split ("##" )[0 ])
57+ return None
58+
59+ def detect_end_of_execution (self , line ):
60+ return "##end_of_execution##" in line or "##execution_error##" in line
0 commit comments