From 6c0f7ba9a73539b46107d13931b7f3159451a423 Mon Sep 17 00:00:00 2001 From: srz_zumix Date: Wed, 3 Aug 2022 22:14:54 +0900 Subject: [PATCH] support Ruby statement --- samples/command/src/ruby/sample.rb | 3 ++- samples/command/src/ruby/test3.rb | 4 ++++ wandbox/__ruby__.py | 23 +++++++++++++---------- 3 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 samples/command/src/ruby/test3.rb diff --git a/samples/command/src/ruby/sample.rb b/samples/command/src/ruby/sample.rb index 438676f..3cfbb7c 100644 --- a/samples/command/src/ruby/sample.rb +++ b/samples/command/src/ruby/sample.rb @@ -1,11 +1,12 @@ # This file is a "Hello, world!" in Ruby language by ruby for wandbox. require './test1' -require_relative 'test2' +require_relative 'test2'; require './test3' puts "Hello, Wandbox!" test1 test2 +test3 # Ruby reference: # https://docs.ruby-lang.org/ diff --git a/samples/command/src/ruby/test3.rb b/samples/command/src/ruby/test3.rb new file mode 100644 index 0000000..4b2a6d5 --- /dev/null +++ b/samples/command/src/ruby/test3.rb @@ -0,0 +1,4 @@ + +def test3 + puts "Test3" +end diff --git a/wandbox/__ruby__.py b/wandbox/__ruby__.py index f2bc7c2..8283841 100644 --- a/wandbox/__ruby__.py +++ b/wandbox/__ruby__.py @@ -3,12 +3,13 @@ from .cli import CLI from .runner import Runner +from .utils import split_statements class RubyRunner(Runner): - REQUIRE_REGEX = re.compile(r'^\s*require\s+(.*?)$') - REQUIRE_RELATIVE_REGEX = re.compile(r'^\s*require_relative\s+(.*?)$') + REQUIRE_REGEX = re.compile(r'^\s*require\s+(.*?)\s*(;|)\s*$') + REQUIRE_RELATIVE_REGEX = re.compile(r'^\s*require_relative\s+(.*?)\s*(;|)\s*$') def reset(self): self.required = [] @@ -17,16 +18,18 @@ def make_code(self, file, filepath, filename): files = dict() code = '' for line in file: - m = self.REQUIRE_REGEX.match(line) - if m: - module = m.group(1).strip('\'"') - if module.startswith('.'): - files.update(self.require(os.path.dirname(filepath), module.strip())) - else: - m = self.REQUIRE_RELATIVE_REGEX.match(line) + statements = split_statements(line, commenters="#") + for statement in statements: + m = self.REQUIRE_REGEX.match(statement) if m: module = m.group(1).strip('\'"') - files.update(self.require(os.path.dirname(filepath), module.strip())) + if module.startswith('.'): + files.update(self.require(os.path.dirname(filepath), module.strip())) + else: + m = self.REQUIRE_RELATIVE_REGEX.match(statement) + if m: + module = m.group(1).strip('\'"') + files.update(self.require(os.path.dirname(filepath), module.strip())) code += line files[filename] = code return files