Created
April 12, 2012 17:23
-
-
Save PatrickTulskie/2369320 to your computer and use it in GitHub Desktop.
Generate some C, compile it, and run it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just because you can do something like this doesn't mean you should. | |
# Executing code via backticks using input from the user is a good way to wreck the host machine. | |
class GenerateSomeC | |
attr_accessor :program_name | |
def initialize(program_name) | |
@program_name = program_name | |
end | |
def generate_app! | |
File.open("#{@program_name}.c", "w") do |file| | |
file.puts generated_code | |
end | |
if `gcc #{@program_name}.c -o #{@program_name}`.chomp.length == 0 | |
puts `./#{program_name}` | |
else | |
puts "Failed to compile :(" | |
end | |
end | |
private | |
def generated_code | |
_c = <<-CODE | |
#include <stdio.h> | |
int main () { | |
printf("Hello, my name is #{program_name}"); | |
return 1; | |
} | |
CODE | |
end | |
end | |
generator = GenerateSomeC.new("patrick") | |
generator.generate_app! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment