Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ruby.h>
- #include <stdbool.h>
- VALUE ruby_protect_eval_string_rescue(VALUE exception, VALUE exception_object)
- {
- *(bool *)exception = true;
- fprintf(stderr, "exception: %s\n",
- rb_obj_classname(exception_object));
- return Qnil;
- }
- VALUE ruby_protect_eval_string(const char * ruby_expression, bool * exception)
- {
- *exception = false;
- return rb_rescue(rb_eval_string, (VALUE)ruby_expression,
- ruby_protect_eval_string_rescue, (VALUE)exception);
- }
- int main(int argc, char **argv)
- {
- if (argc < 2)
- {
- fprintf(stderr, "Usage: %s <ruby-expression>\n", argv[0]);
- return -1;
- }
- VALUE v = INT2FIX(0);
- bool exception;
- {
- RUBY_INIT_STACK;
- ruby_init();
- v = ruby_protect_eval_string(argv[1], &exception);
- }
- if (exception)
- {
- fprintf(stderr, "Exception!\n");
- }
- else
- {
- if (FIXNUM_P(v))
- {
- printf("Success! Result is %d\n", FIX2INT(v));
- }
- else
- {
- printf("Success! Non-Fixnum result\n");
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment