How to make an unix library from some ruby code?

Hello *,

is there any possibility to build an unix library not from C/C++/…
code, but from ruby code? IOW, I need to have some functionality
“packaged” as an unix library, but I’d like to write it in Ruby and
then, somehow, compile it. Is it possible?

Thanks,

P.

I need to have some functionality “packaged” as an unix library
Just write the ruby code or use what is already available.
Ruby can do the same what the unix tools can. Its just a question of
how much work to invest :slight_smile:

but I’d like to write it in Ruby and then, somehow, compile it.
Compile ruby code? That is not really possible. It’s just .rb files.

Is it possible?
I dont think it is.

On 17.03.2008 13:44, Marc H. wrote:

but I’d like to write it in Ruby and then, somehow, compile it.
Compile ruby code? That is not really possible. It’s just .rb files.

Actually you can compile Ruby to an executable. I am just not sure
whether compiling it to a library works as well.

Kind regards

robert

Marc H. wrote:

I need to have some functionality “packaged” as an unix library
Just write the ruby code or use what is already available.
Ruby can do the same what the unix tools can. Its just a question of
how much work to invest :slight_smile:

Unfortunately, I definitely need a library which could be easily used in
C programs (or elsewhere). A ruby script does not fit my needs.

Because it is possible to embed Perl interpreter to a C program (man
perlembed, perlembed - how to embed perl in your C program - Perldoc Browser), I believe there
could be something similar in Ruby.

Thanks, P.

Pavel,

perlembed is about embedding Perl into your programs, not libraries
and even then it’s about building a C / C++ program that can call out
perl functions, like for using Perl as a scripting language. If you
want a library that’s linked to by other programs, it has to be
written in a language that can be compiled into a static library.
Thus, you’re stuck with C / C++.

What you want, you can’t do, whether in Ruby, Perl, Lua, etc.

You can fake it by:

Creating a C library with a single function that starts up the Ruby vm
and executes installed Ruby scripts or Ruby code compiled into the
library as a giant string.
Link to that library, link to Ruby, and then call the function to
start the whole process.

Please don’t do this. If it needs to be a Unix library, just write it
in C / C++.

Jason

Pavel S. [email protected] writes:

perlembed, perlembed - how to embed perl in your C program - Perldoc Browser), I believe there
could be something similar in Ruby.

But unix provides all that is needed to do such a thing.

/* pseudo-code */

typedef struct ruby{
int to_ruby;
int from_ruby;
} ruby;

ruby* init_ruby(){
int to_ruby[2];
int from_ruby[2];
int res=0;
res=pipe(to_ruby); if(res!=0){perror(“pipe”);exit(1);}
res=pipe(from_ruby); if(res!=0){perror(“pipe”);exit(1);}
if(0==fork()){
close(to_ruby[0]);
close(from_ruby[1]);
ruby* r=malloc(sizeof(ruby));
r->to_ruby=to_ruby[1];
r->from_ruby=from_ruby[0];
return(r);
}else{
close(to_ruby[0]);
close(from_ruby[1]);
close(0);
close(1);
dup2(to_ruby[1],0);
dup2(from_ruby[0],1);
execl(“/usr/bin/irb”);
exit(0);
}
}

char* ruby_eval(ruby* r,const char* e){
while(strlen(e)>0){
int wrote=write(r->to_ruby,e,strlen(e));
if(wrote<0){
perror(“write to ruby”);
exit(1);
}else{
e+=wrote;
}
}
{
int bufsize=65536;
char* buffer=malloc(bufsize);
int readed=read(r->from_ruby,buffer,bufsize-1);
if(readed<0){
/* actually, should check for EAGAIN, etc */
perror(“read from ruby”);
}
buffer[readed]=0;
return(buffer);
}
}

so now you can write:

ruby* r=init_ruby();
printf(“%s\n”,ruby_eval(r,“[1,2,3].length;\n”););
// etc…

You don’t need a library for just two routines, I would think…

On Mon, Mar 17, 2008 at 6:43 AM, Jason R. [email protected]
wrote:

Please don’t do this. If it needs to be a Unix library, just write it
in C / C++.

D might be a nice compromise between C and Ruby here.

martin

“P” == Pascal J Bourguignon [email protected] writes:

P> /* pseudo-code */

???

P> ruby* init_ruby(){

vgs% grep -w ruby_init ruby.h
void ruby_init _((void));
vgs%

P> char* ruby_eval(ruby* r,const char* e){

vgs% grep rb_eval_string_protect ruby.h
VALUE rb_eval_string_protect _((const char*, int*));
vgs%

Guy Decoux

Not having done this, this is a shot in the dark, but…

First thing to do, is to see if you can use compiled ruby extensions
from a plain old c program.
Pick a common compiled ruby lib, and try and link to it. Maybe
digest.so? I’m pretty sure you can get this to work, but I’ve never
tried.

Now…
There are several systems out there to make it easier for you to
develop ruby code, and turn it into a c extension for ruby: rubyinline
and ruby2c come to mind (ruby2c may be slightly abandoned, but it
could still help).

This is the only way that comes to my mind to do what you want to do…

Write your library in pure ruby
Write a really good set of unit tests!
Use those tools to piecewise convert the library to a purely C ruby
extension
Make sure those unit tests still pass…
Link to that extension (probably libruby too) from your C program.

–Kyle