code-runner

Run code in 40+ programming languages – simple, fast, and perfect for learning!

Gem Version GitHub Stars MIT License Downloads


Features

Here's what you can do with Code-Runner:

  • 40+ languages – Python, Ruby, JavaScript, Java, C++, Go, Rust, and many more
  • Instant execution – Write code, run it, see results. No setup needed
  • Pass input – Your code can read from stdin, just like a real program
  • Command line args – Test how your script handles arguments
  • Timeouts – No more infinite loops crashing your app
  • Clean output – Get stdout, stderr, and compiler messages separately


Installation

gem install code-runner

require 'code-runner'

Ruby

result = CodeRunner.run('puts "Hello World!"', 'ruby') puts result[:output] # Hello World!

Python

result = CodeRunner.run('print("Hello Python!")', 'python') puts result[:output] # Hello Python!

JavaScript

result = CodeRunner.run('console.log("Hello JS!")', 'javascript') puts result[:output] # Hello JS!

Examples

1. Simple math

ruby

Ruby

result = CodeRunner.run('puts 2 + 2', 'ruby') puts result[:output] # 4

Python

result = CodeRunner.run('print(3 * 4)', 'python') puts result[:output] # 12

2. Reading input (stdin)

ruby

code = <<~PYTHON name = input("What's your name? ") print(f"Hello name!") PYTHON

result = CodeRunner.run(code, 'python', stdin: "John") puts result[:output]

What's your name? Hello John!

3. Command line arguments

ruby

code = 'puts "Args: #")"' result = CodeRunner.run(code, 'ruby', args: ["hello", "world"]) puts result[:output] # Args: hello, world

4. Error handling

ruby

result = CodeRunner.run('print(1 / 0)', 'python')

if result[:success] puts "ok : #result[:output]" else puts "err : #result[:error]" end

Supported Languages

full list:

Python Ruby JavaScript Java C C++ C# PHP Go Rust Bash Swift Kotlin TypeScript Dart R Perl Lua Haskell Elixir Erlang Clojure Scala Fortran Julia Groovy

📬 Get in Touch

I'd love to hear from you! Here's how you can reach me:

GitHub monji024
RubyGems code-runner

Show Your Support

If you find this gem useful, here's how you can support me:

⭐ Star it on GitHub

Visit github.com/monji024/code-runner and click the ★ button.