Skip to content

Commit 90b19b5

Browse files
authored
Merge pull request #1105 from bars0um/feature/ruby-support
Feature/ruby support
2 parents fd4b8f6 + 11f8749 commit 90b19b5

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

interpreter/core/computer/terminal/terminal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .languages.python import Python
77
from .languages.r import R
88
from .languages.react import React
9+
from .languages.ruby import Ruby
910
from .languages.shell import Shell
1011

1112
# Should this be renamed to OS or System?
@@ -15,6 +16,7 @@ class Terminal:
1516
def __init__(self, computer):
1617
self.computer = computer
1718
self.languages = [
19+
Ruby,
1820
Python,
1921
Shell,
2022
JavaScript,

0 commit comments

Comments
 (0)