I have one text area where the user types in a basic C program such as printing Hello World or addition of two numbers. Now, I want the user to be able to click on a button so that whatever code he types gets compiled and its output is shown onto another text area.

I am a beginner to rails and don't know how to implement this. A link to any tutorial for the same will be appreciated. I tried searching on google but was unable to find a suitable solution.

I am looking for something similar to shell_exec for PHP. For example, in PHP

<?php
shell_exec('gcc myfile.c -o a');
shell_exec('./a');
?>
有帮助吗?

解决方案

There are a couple of ways to do this in ruby:

1, Backticks

`gcc myfile.c -o a`
`./a`

2, system

system("gcc myfile.c -o a")  
system("./a")

3, [IO#popen](See http://www.ruby-doc.org/core-2.1.0/IO.html#method-c-popen)

IO.popen("gcc myfile.c -o a")
IO.popen("("gcc myfile.c -o a")

N.B: There are more ways to do this (IO:popen3, exec...)

其他提示

There is a great Ruby Quicktips article on that topic: Execute shell commands.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top