Skip to content

Instantly share code, notes, and snippets.

@PatrickTulskie
Created April 12, 2012 17:23
Show Gist options
  • Save PatrickTulskie/2369320 to your computer and use it in GitHub Desktop.
Save PatrickTulskie/2369320 to your computer and use it in GitHub Desktop.
Generate some C, compile it, and run it.
# 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