ruby-code-runner 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +148 -0
- data/lib/code-runner.rb +119 -0
- metadata +87 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 112b195a4e4e6d6b0583b278f22dddf1c0cbbbc6631bc9b8d67f6d77cc863bd9
|
|
4
|
+
data.tar.gz: 80fda992b8b79eeb0ffe899cb3726792c9ed887a1f2adfcb4a89d7d48b6becdc
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 03e69e8d62b60d43775a13734b4edb1d4a145e8fa4058c7439e7f43efd20ed45c3dcf6455d8deacf563c6ccaca402dcf26da117c53c4de851e84878016d1b133
|
|
7
|
+
data.tar.gz: 64b0a6db03c8bf6aa27647ccdba584067030d606089b6e09fd3fb2f8c2af8bfdb32ee3aaedd9c4b6fd0c8ade5faa7b95845989a8c387c92a992015ddb4e9a847
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Monji
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>code-runner</h1>
|
|
3
|
+
<p><strong>Run code in 40+ programming languages – simple, fast, and perfect for learning!</strong></p>
|
|
4
|
+
|
|
5
|
+
<a href="https://rubygems.org/gems/code-runner">
|
|
6
|
+
<img src="https://img.shields.io/gem/v/code-runner?color=red&style=flat-square" alt="Gem Version">
|
|
7
|
+
</a>
|
|
8
|
+
<a href="https://github.com/monji024/code-runner">
|
|
9
|
+
<img src="https://img.shields.io/github/stars/monji024/code-runner?style=flat-square" alt="GitHub Stars">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/monji024/code-runner/blob/main/LICENSE">
|
|
12
|
+
<img src="https://img.shields.io/github/license/monji024/code-runner?style=flat-square" alt="MIT License">
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://rubygems.org/gems/code-runner">
|
|
15
|
+
<img src="https://img.shields.io/gem/dt/code-runner?style=flat-square" alt="Downloads">
|
|
16
|
+
</a>
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
<br>
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
Here's what you can do with Code-Runner:
|
|
24
|
+
|
|
25
|
+
- **40+ languages** – Python, Ruby, JavaScript, Java, C++, Go, Rust, and many more
|
|
26
|
+
- **Instant execution** – Write code, run it, see results. No setup needed
|
|
27
|
+
- **Pass input** – Your code can read from stdin, just like a real program
|
|
28
|
+
- **Command line args** – Test how your script handles arguments
|
|
29
|
+
- **Timeouts** – No more infinite loops crashing your app
|
|
30
|
+
- **Clean output** – Get stdout, stderr, and compiler messages separately
|
|
31
|
+
|
|
32
|
+
<br>
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
gem install code-runner
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
require 'code-runner'
|
|
41
|
+
|
|
42
|
+
# Ruby
|
|
43
|
+
result = CodeRunner.run('puts "Hello World!"', 'ruby')
|
|
44
|
+
puts result[:output] # Hello World!
|
|
45
|
+
|
|
46
|
+
# Python
|
|
47
|
+
result = CodeRunner.run('print("Hello Python!")', 'python')
|
|
48
|
+
puts result[:output] # Hello Python!
|
|
49
|
+
|
|
50
|
+
# JavaScript
|
|
51
|
+
result = CodeRunner.run('console.log("Hello JS!")', 'javascript')
|
|
52
|
+
puts result[:output] # Hello JS!
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
Examples
|
|
56
|
+
## 1. Simple math
|
|
57
|
+
ruby
|
|
58
|
+
|
|
59
|
+
# Ruby
|
|
60
|
+
result = CodeRunner.run('puts 2 + 2', 'ruby')
|
|
61
|
+
puts result[:output] # 4
|
|
62
|
+
|
|
63
|
+
# Python
|
|
64
|
+
result = CodeRunner.run('print(3 * 4)', 'python')
|
|
65
|
+
puts result[:output] # 12
|
|
66
|
+
|
|
67
|
+
## 2. Reading input (stdin)
|
|
68
|
+
ruby
|
|
69
|
+
|
|
70
|
+
code = <<~PYTHON
|
|
71
|
+
name = input("What's your name? ")
|
|
72
|
+
print(f"Hello {name}!")
|
|
73
|
+
PYTHON
|
|
74
|
+
|
|
75
|
+
result = CodeRunner.run(code, 'python', stdin: "John")
|
|
76
|
+
puts result[:output]
|
|
77
|
+
# What's your name? Hello John!
|
|
78
|
+
|
|
79
|
+
## 3. Command line arguments
|
|
80
|
+
ruby
|
|
81
|
+
|
|
82
|
+
code = 'puts "Args: #{ARGV.join(", ")}"'
|
|
83
|
+
result = CodeRunner.run(code, 'ruby', args: ["hello", "world"])
|
|
84
|
+
puts result[:output] # Args: hello, world
|
|
85
|
+
|
|
86
|
+
## 4. Error handling
|
|
87
|
+
ruby
|
|
88
|
+
|
|
89
|
+
result = CodeRunner.run('print(1 / 0)', 'python')
|
|
90
|
+
|
|
91
|
+
if result[:success]
|
|
92
|
+
puts "ok : #{result[:output]}"
|
|
93
|
+
else
|
|
94
|
+
puts "err : #{result[:error]}"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
## Supported Languages
|
|
99
|
+
|
|
100
|
+
full list:
|
|
101
|
+
|
|
102
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" width="16" height="16"> Python
|
|
103
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" width="16" height="16"> Ruby
|
|
104
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/javascript/javascript-original.svg" width="16" height="16"> JavaScript
|
|
105
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/java/java-original.svg" width="16" height="16"> Java
|
|
106
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/c/c-original.svg" width="16" height="16"> C
|
|
107
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/cplusplus/cplusplus-original.svg" width="16" height="16"> C++
|
|
108
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/csharp/csharp-original.svg" width="16" height="16"> C#
|
|
109
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/php/php-original.svg" width="16" height="16"> PHP
|
|
110
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/go/go-original.svg" width="16" height="16"> Go
|
|
111
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/rust/rust-original.svg" width="16" height="16"> Rust
|
|
112
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/bash/bash-original.svg" width="16" height="16"> Bash
|
|
113
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/swift/swift-original.svg" width="16" height="16"> Swift
|
|
114
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/kotlin/kotlin-original.svg" width="16" height="16"> Kotlin
|
|
115
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/typescript/typescript-original.svg" width="16" height="16"> TypeScript
|
|
116
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/dart/dart-original.svg" width="16" height="16"> Dart
|
|
117
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/r/r-original.svg" width="16" height="16"> R
|
|
118
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/perl/perl-original.svg" width="16" height="16"> Perl
|
|
119
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/lua/lua-original.svg" width="16" height="16"> Lua
|
|
120
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/haskell/haskell-original.svg" width="16" height="16"> Haskell
|
|
121
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/elixir/elixir-original.svg" width="16" height="16"> Elixir
|
|
122
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/erlang/erlang-original.svg" width="16" height="16"> Erlang
|
|
123
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/clojure/clojure-original.svg" width="16" height="16"> Clojure
|
|
124
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/scala/scala-original.svg" width="16" height="16"> Scala
|
|
125
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/fortran/fortran-original.svg" width="16" height="16"> Fortran
|
|
126
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/julia/julia-original.svg" width="16" height="16"> Julia
|
|
127
|
+
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/groovy/groovy-original.svg" width="16" height="16"> Groovy
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# 📬 Get in Touch
|
|
133
|
+
|
|
134
|
+
I'd love to hear from you! Here's how you can reach me:
|
|
135
|
+
|
|
136
|
+
| | |
|
|
137
|
+
|---|---|
|
|
138
|
+
| **GitHub** | [monji024](https://github.com/monji024) |
|
|
139
|
+
| **RubyGems** | [code-runner](https://rubygems.org/gems/code-runner) |
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
# Show Your Support
|
|
144
|
+
|
|
145
|
+
If you find this gem useful, here's how you can support me:
|
|
146
|
+
|
|
147
|
+
## ⭐ Star it on GitHub
|
|
148
|
+
Visit [github.com/monji024/code-runner](https://github.com/monji024/code-runner) and click the ★ button.
|
data/lib/code-runner.rb
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# lib/code-runner.rb
|
|
2
|
+
require 'httparty'
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module CodeRunner
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
SUPPORTED_LANGUAGES = {
|
|
9
|
+
'python' => 'python', 'py' => 'python',
|
|
10
|
+
'javascript' => 'javascript', 'js' => 'javascript',
|
|
11
|
+
'java' => 'java',
|
|
12
|
+
'cpp' => 'cpp', 'c' => 'c',
|
|
13
|
+
'csharp' => 'csharp', 'cs' => 'csharp',
|
|
14
|
+
'ruby' => 'ruby', 'rb' => 'ruby',
|
|
15
|
+
'php' => 'php',
|
|
16
|
+
'go' => 'go',
|
|
17
|
+
'rust' => 'rust', 'rs' => 'rust',
|
|
18
|
+
'kotlin' => 'kotlin', 'kt' => 'kotlin',
|
|
19
|
+
'swift' => 'swift',
|
|
20
|
+
'typescript' => 'typescript', 'ts' => 'typescript',
|
|
21
|
+
'bash' => 'bash', 'sh' => 'bash',
|
|
22
|
+
'r' => 'r',
|
|
23
|
+
'perl' => 'perl', 'pl' => 'perl',
|
|
24
|
+
'lua' => 'lua',
|
|
25
|
+
'haskell' => 'haskell', 'hs' => 'haskell',
|
|
26
|
+
'elixir' => 'elixir', 'exs' => 'elixir',
|
|
27
|
+
'clojure' => 'clojure', 'clj' => 'clojure',
|
|
28
|
+
'dart' => 'dart',
|
|
29
|
+
'assembly' => 'assembly', 'asm' => 'assembly',
|
|
30
|
+
'fortran' => 'fortran', 'f90' => 'fortran',
|
|
31
|
+
'julia' => 'julia', 'jl' => 'julia',
|
|
32
|
+
'cobol' => 'cobol', 'cbl' => 'cobol',
|
|
33
|
+
'vb' => 'vbnet', 'vba' => 'vbnet',
|
|
34
|
+
'scala' => 'scala',
|
|
35
|
+
'racket' => 'racket', 'rkt' => 'racket',
|
|
36
|
+
'erlang' => 'erlang', 'erl' => 'erlang',
|
|
37
|
+
'fsharp' => 'fsharp', 'fs' => 'fsharp',
|
|
38
|
+
'groovy' => 'groovy', 'gvy' => 'groovy',
|
|
39
|
+
'powershell' => 'powershell', 'ps1' => 'powershell',
|
|
40
|
+
'sql' => 'sql'
|
|
41
|
+
}.freeze
|
|
42
|
+
|
|
43
|
+
API_URL = "https://emkc.org/api/v2/piston/execute".freeze
|
|
44
|
+
|
|
45
|
+
def self.run(code, language, options = {})
|
|
46
|
+
piston_lang = normalize_language(language)
|
|
47
|
+
raise Error, "Language '#{language}' not supported" unless piston_lang
|
|
48
|
+
|
|
49
|
+
payload = {
|
|
50
|
+
language: piston_lang,
|
|
51
|
+
version: options[:version] || "*",
|
|
52
|
+
files: [{ content: code }],
|
|
53
|
+
stdin: options[:stdin] || "",
|
|
54
|
+
args: options[:args] || [],
|
|
55
|
+
compile_timeout: options[:compile_timeout] || 10000,
|
|
56
|
+
run_timeout: options[:run_timeout] || 5000,
|
|
57
|
+
compile_memory_limit: options[:compile_memory_limit] || -1,
|
|
58
|
+
run_memory_limit: options[:run_memory_limit] || -1
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
response = HTTParty.post(API_URL,
|
|
62
|
+
body: payload.to_json,
|
|
63
|
+
headers: { 'Content-Type' => 'application/json' },
|
|
64
|
+
timeout: options[:timeout] || 30
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
unless response.success?
|
|
68
|
+
raise Error, "API Error: #{response.code} - #{response.message}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
parse_response(response.parsed_response)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.execute(code, language, options = {})
|
|
75
|
+
run(code, language, options)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def self.eval_code(code, language, options = {})
|
|
79
|
+
run(code, language, options)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.supported_languages
|
|
83
|
+
SUPPORTED_LANGUAGES.keys.uniq.sort
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def self.supported?(language)
|
|
87
|
+
!normalize_language(language).nil?
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def self.version
|
|
91
|
+
"1.0.2"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
def self.normalize_language(lang)
|
|
97
|
+
SUPPORTED_LANGUAGES[lang.to_s.downcase]
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def self.parse_response(data)
|
|
101
|
+
output = data.dig('run', 'output') || ''
|
|
102
|
+
error = data.dig('run', 'stderr') || ''
|
|
103
|
+
compile_output = data.dig('compile', 'output') || ''
|
|
104
|
+
|
|
105
|
+
output = output.force_encoding('UTF-8') if output.encoding != Encoding::UTF_8
|
|
106
|
+
error = error.force_encoding('UTF-8') if error.encoding != Encoding::UTF_8
|
|
107
|
+
compile_output = compile_output.force_encoding('UTF-8') if compile_output.encoding != Encoding::UTF_8
|
|
108
|
+
|
|
109
|
+
{
|
|
110
|
+
success: data.dig('run', 'code') == 0,
|
|
111
|
+
output: output,
|
|
112
|
+
error: error,
|
|
113
|
+
compile_output: compile_output,
|
|
114
|
+
language: data['language'],
|
|
115
|
+
version: data['version'],
|
|
116
|
+
code: data.dig('run', 'code')
|
|
117
|
+
}
|
|
118
|
+
end
|
|
119
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ruby-code-runner
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Monji
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2026-02-13 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: httparty
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.21'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.21'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: minitest
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '5.18'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '5.18'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rake
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '13.0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '13.0'
|
|
54
|
+
description: a simple gem to compile and execute code in 40+ programming languages
|
|
55
|
+
email: hoseinmonjiofficial@gmail.com
|
|
56
|
+
executables: []
|
|
57
|
+
extensions: []
|
|
58
|
+
extra_rdoc_files: []
|
|
59
|
+
files:
|
|
60
|
+
- LICENSE
|
|
61
|
+
- README.md
|
|
62
|
+
- lib/code-runner.rb
|
|
63
|
+
homepage: https://github.com/monji024/code-runner
|
|
64
|
+
licenses:
|
|
65
|
+
- MIT
|
|
66
|
+
metadata:
|
|
67
|
+
source_code_uri: https://github.com/monji024/code-runner
|
|
68
|
+
changelog_uri: https://github.com/monji024/code-runner/blob/main/CHANGELOG.md
|
|
69
|
+
rubygems_mfa_required: 'false'
|
|
70
|
+
rdoc_options: []
|
|
71
|
+
require_paths:
|
|
72
|
+
- lib
|
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: 2.6.0
|
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
requirements: []
|
|
84
|
+
rubygems_version: 3.6.3
|
|
85
|
+
specification_version: 4
|
|
86
|
+
summary: Run code in 40+ languages
|
|
87
|
+
test_files: []
|