[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to make an unix library from some ruby code?

Pavel Smerk

3/17/2008 11:40:00 AM

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

Marc Heiler

3/17/2008 12:45:00 PM

0

> 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.
--
Posted via http://www.ruby-....

Robert Klemme

3/17/2008 12:54:00 PM

0

On 17.03.2008 13:44, Marc Heiler 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

Pavel Smerk

3/17/2008 1:17:00 PM

0

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

Thanks, P.

pjb

3/17/2008 1:43:00 PM

0

Pavel Smerk <smerk@fi.muni.cz> writes:

> Marc Heiler 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, http://perldoc.perl.org/perl...), 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...

--
__Pascal Bourguignon__

Jason Roelofs

3/17/2008 1:44:00 PM

0

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

On Mon, Mar 17, 2008 at 9:24 AM, Pavel Smerk <smerk@fi.muni.cz> wrote:
> Marc Heiler 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, http://perldoc.perl.org/perl...), I believe there
> could be something similar in Ruby.
>
> Thanks, P.
>
>

Martin DeMello

3/18/2008 6:36:00 PM

0

On Mon, Mar 17, 2008 at 6:43 AM, Jason Roelofs <jameskilton@gmail.com> 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

Kyle Schmitt

3/18/2008 7:03:00 PM

0

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

On Mon, Mar 17, 2008 at 6:54 AM, Pavel Smerk <smerk@fi.muni.cz> wrote:
> 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.
>
>