# How to make an unix library from some ruby code?

**URL:** <https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148>\
**Category:** Ruby\
**Created:** [March 17, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148 "2008-03-17T12:55:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [March 17, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148/1 "2008-03-17T12:55:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [March 17, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148/2 "2008-03-17T13:44:40Z")

</div>

> 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 🙂

> 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.

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [March 17, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148/3 "2008-03-17T13:55:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [March 17, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148/4 "2008-03-17T14:26:09Z")

</div>

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 🙂

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](http://perldoc.perl.org/perlembed.html)), I believe there  
could be something similar in Ruby.

Thanks, P.

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [March 17, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148/5 "2008-03-17T14:44:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [March 17, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148/6 "2008-03-17T14:46:27Z")

</div>

Pavel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> perlembed, [perlembed - how to embed perl in your C program - Perldoc Browser](http://perldoc.perl.org/perlembed.html)), 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…

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [March 18, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148/7 "2008-03-18T19:36:51Z")

</div>

On Mon, Mar 17, 2008 at 6:43 AM, Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [March 17, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148/8 "2008-03-17T14:52:57Z")

</div>

> > > > > “P” == Pascal J Bourguignon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [March 18, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/how-to-make-an-unix-library-from-some-ruby-code/132148/9 "2008-03-18T20:04:02Z")

</div>

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
