Created
August 11, 2011 12:37
-
-
Save kbarber/1139540 to your computer and use it in GitHub Desktop.
Running Ruby code in C
This file contains 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
#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