How to host Ruby in C

Hi. Having googled extensively, I understand that I can host a Ruby
environment in C using the ruby_init(), ruby_run() and ruby_finalize()
calls.

The problem I’m having concerns injecting the actual code. It would
appear that I can provide a .rb file using rb_load_file(), or a single
line of code using rb_eval_string(). What I want to do, however, is
provide a whole script as a string.

Presumably rb_eval_string() wouldn’t accept a whole script at once? And
I don’t want to write a temporary file. Maybe I could call
rb_eval_string() once with each line of the script…would the
variables and such persist across multiple calls?

I’d prefer to pass the entire script all at once, if possible.

FYI, the purpose of all this is to essentially compile a script, by
appending said script to the end of a simple C executable, which reads
the script out of its own backside and executes it from memory.

Presumably rb_eval_string() wouldn’t accept a whole script at once?

Have you tried it?

http://d.hatena.ne.jp/mscp/touch/20090923/1261918364

(Japanese, but the code is self-explanatory.)

Dansei Yuuki wrote in post #1174976:

Presumably rb_eval_string() wouldn’t accept a whole script at once?

Have you tried it?

…no? :wink:

So it’s that simple huh? Well, guess I look like a bit of a fool now.

Thanks for the help.

I’ve written a small test program, but can’t compile it:

ruby-host.c:(.text+0x5): undefined reference to ruby_init' ruby-host.c:(.text+0x14): undefined reference toruby_script’
ruby-host.c:(.text+0x1e): undefined reference to `ruby_run’
collect2: error: ld returned 1 exit status

I’ve tried adding “-L/usr/lib/x86_64-linux-gnu -llibruby-2.1.so” to the
gcc command, but it doesn’t help:

/usr/bin/ld: cannot find -llibruby-2.1.so

What am I doing wrong?

I’ve figured out the correct options to give gcc, but I’m still getting
an error. The command I’m using is:

gcc -I/usr/include/ruby-2.1.0
-I/usr/include/x86_64-linux-gnu/ruby-2.1.0 ruby-host.c -lruby-2.1 -o
test

And the error is:

ruby-host.c:(.text+0x1e): undefined reference to `ruby_run’
collect2: error: ld returned 1 exit status

i had consulted ruby on rails experts in india and they are going to
explain me about this… you can mail me on [email protected]

Well? Nobody’s had any idea how to solve this in three damn months?

S. B. wrote in post #1178453:
Well? Nobody’s had any idea how to solve this in three damn months?

Not everyone knows C.

Additionally old threads/mails quickly phase away.

You could give stackoverflow a try, their system is usually better in
the long run due to archiving + karma/upvotes.

C is pretty much the most well-known programming language in the world.
Some coders might not like using it, but they should still know how it’s
done. Like me and Java: Java is a godawfully bad language, but you still
need a vague understanding of it.

Thanks, I’ll try Stack Overflow.

Robert H. wrote in post #1178823:

S. B. wrote in post #1178453:
Well? Nobody’s had any idea how to solve this in three damn months?

Not everyone knows C.

Additionally old threads/mails quickly phase away.

You could give stackoverflow a try, their system is usually better in
the long run due to archiving + karma/upvotes.