Skip to content

monji024/ruby-code-runner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ruby-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 ruby-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!

1. Simple math

# 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)

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

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

What's your name? Hello Monji!

3. Command line arguments

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

4. Error handling

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

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages