Skip to content

Instantly share code, notes, and snippets.

@kbarber
Created August 11, 2011 12:37
Show Gist options
  • Save kbarber/1139540 to your computer and use it in GitHub Desktop.
Save kbarber/1139540 to your computer and use it in GitHub Desktop.
Running Ruby code in C
#include <stdio.h>
#include <ruby.h>
int main(int argc, char *argv[]) {
// Open file
FILE *fp;
fp = fopen(argv[1], "r");
if(fp == NULL) {
return 1;
}
// Size of file
fseek(fp, 0, SEEK_END);
long int size = ftell(fp);
rewind(fp);
// File -> string
char* content = calloc(size + 1, 1);
fread(content,1,size,fp);
// Run the ruby
ruby_init();
rb_eval_string(content);
ruby_finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment