rb_eval_string function, which executes a string with Ruby code.($ a + $ b) * $ a- $ b 2 3
($ a + $ b) * $ a- $ b = 7
#include <ruby.h>
#include <stdio.h>
int main () {
ruby_init (); // initialization of the interpreter
int a = 0;
int b = 0;
char expr [256];
// read the expression and two variables
scanf ("% s", & expr);
scanf ("% d", & a);
scanf ("% d", & b);
// create ruby variables
VALUE r_a = INT2NUM (a);
VALUE r_b = INT2NUM (b);
// and make them available in the interpreter
rb_define_variable ("$ a", & r_a);
rb_define_variable ("$ b", & r_b);
// execute the expression and print the result
VALUE res = rb_eval_string (expr);
printf ("% s =% d \ n", expr, NUM2INT (res));
return 0;
}
rb_define_variable method, made them available in the interpreter.rb_eval_string method, we executed the expression and displayed its result on the screen. #include <ruby.h>
#include <stdio.h>
int main () {
ruby_init (); // initialization of the interpreter
ruby_init_loadpath (); // ability to include standard libraries, for example require 'MD5'
VALUE res = rb_str_new2 ("some test string"); // our test string
rb_define_variable ("$ res", & res); // make it available to the script
rb_load_file ("alg.rb"); // load the file with the algorithm
// execute the script and print the result
ruby_exec ();
printf ("% s \ n", StringValuePtr (res));
return 0;
}
hash = 0
$ res.split (//). each {| c | hash + = 1 if c == ""}
$ res = hash.to_s
hash = 0
$ res.split (//). each {| c | hash + = c [0]}
$ res = hash.to_s (16)
require "md5" $ res = MD5.hexdigest ($ res)
gcc test.c -o test -I< ruby.h> -lrubygcc test.c -o test -I< ruby.h> -lruby1.8sudo apt-get install ruby1.8-devSource: https://habr.com/ru/post/50039/
All Articles