Compiling C++ code stored in a Ruby string without writing it into a file -


i'm writing ruby code generates string containing c++ program. instance ruby string may contain:

#include <iostream> using namespace std;int main(){cout<<"hello world"<<endl;return 0;} 

in order run c++ program stored in ruby string, write string file named c_prog.cpp, use:

%x( g++ c_prog.cpp -o output ) 

to compile c++ program in file, use:

value = %x( ./output ) 

and print value.

since c++ program stored in ruby string long (thousands of loc), writing file wastes time. there way can compile program stored in string without writing file? mean like:

%x( g++ 'the ruby string' -o output ) 

instead of:

%x( g++ c_prog.cpp -o output ) 

you can pass in single dash file name tell g++ read stdin. so,

%x( echo 'the ruby string' | g++ -o output -x c++ - ) 

should trick. see this related question.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -