[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby to C to another language (perhaps Java (I Don't Need JRuby

Zach Dennis

11/17/2004 6:42:00 AM

This posting is more for a learning thing then anything else at this
point. I've been playing with Java to C via JNI lately doing some pretty
basic tests; calling methods, getting and settings fields, passing
primitive types around, etc...

A while back (a few months ago) I did some Ruby to C tests of pretty
much the same caliber.

I would like to combine the two approaches and share some objects
between Ruby to C to Java and vise versa. I am not looking for the JVM
to be able to process ruby code. (no JRuby)

Has anyone had a similar approach (even if it wasn't Java) where they
were able to wrap a few Objects and use C as the middle man to communicate?

If so, did you have to embed the ruby compiler inside the *.so or *.dll
file when running the interpretor of the other language (i presume yes?)
and what was the level of difficulty (one word answers are fine. I can
live with easy, hard, difficult, if you want to share a whole paragraph
then please do)?

Is it difficult to share data structures between three different
languages. Again Ruby/C/"3rd Language"?

What I am trying to get out of this is in the long run is to be able to
have a few data structures that I can share between Ruby and Java, via
C. I currently have java application and I feel I could code quicker
certain aspects alot quicker in Ruby if only I could share a data
structure/object. Ideally I would like to do something like:

---In My Ruby Code---
class StringProcessor
def process_str( str )
#process
return process_str
end
end


---In my Java Code----
class StringProcessor{
public native String _processString( String str );
public void processString( String str ){
this._processString( str );
}
public static void main( String[] args ){
new StringProcessor().processString( aBigString );
}
}


-----In my C Code (pseudo)-----
Java_StringProcessor__processString jstring ( jstring str ){
char c = jStringToCharArray( str )
c = Ruby_StringProcessor_processString( c )
return charToJString( c )
}

jStringToCharArray char[] ( jstring str ){
char[] c
//convert
return c
}

charToJString jstring( char[] c )
jstring j
//convert
return j
}

Ruby_StringProcessor_processString char[] ( rstring str ){
char[] c;
//Process
return c;
}



This is not a well thought out example. It's just off the top of my head.

So does anyone know if this is possible?

Thanks,

Zach







9 Answers

Kaspar Schiess

11/17/2004 7:45:00 AM

0

Zach Dennis <zdennis@mktec.com> wrote in
news:419AF224.5070307@mktec.com:

> Has anyone had a similar approach (even if it wasn't Java) where they
> were able to wrap a few Objects and use C as the middle man to
> communicate?

Hello Denis,

I know of rjava (using a tcp connection) and rjb (using a jni native
interface) as far as ruby to java interfacing goes. I have tested rjb and
found it to work (with some tweaking) as I wanted it to. I don't know about
the difficulty of these efforts, but now that you have the source, I guess
duplicating them is a breeze, depending on how much cheating you allow
yourself to do.

best regards, kaspar

hand manufactured code - www.tua.ch/ruby



Lyndon Samson

11/17/2004 10:08:00 AM

0

Sounds like you are better off skipping the C/C++ layer and using
something like shared memory/memory mapped files to communicate
between ruby and java.

You could use real threads, or some sort of co-routine.

Simple solution would be to use XML, otherwise you'll need to write
something that unpacks complex structures from byte arrays.


BTW, your subject line is a little confusing, I thought the post would
be about a Ruby to C translator. :-)


On Wed, 17 Nov 2004 15:42:19 +0900, Zach Dennis <zdennis@mktec.com> wrote:
> This posting is more for a learning thing then anything else at this
> point. I've been playing with Java to C via JNI lately doing some pretty
> basic tests; calling methods, getting and settings fields, passing
> primitive types around, etc...
>
> A while back (a few months ago) I did some Ruby to C tests of pretty
> much the same caliber.
>
> I would like to combine the two approaches and share some objects
> between Ruby to C to Java and vise versa. I am not looking for the JVM
> to be able to process ruby code. (no JRuby)
>
> Has anyone had a similar approach (even if it wasn't Java) where they
> were able to wrap a few Objects and use C as the middle man to communicate?
>
> If so, did you have to embed the ruby compiler inside the *.so or *.dll
> file when running the interpretor of the other language (i presume yes?)
> and what was the level of difficulty (one word answers are fine. I can
> live with easy, hard, difficult, if you want to share a whole paragraph
> then please do)?
>
> Is it difficult to share data structures between three different
> languages. Again Ruby/C/"3rd Language"?
>
> What I am trying to get out of this is in the long run is to be able to
> have a few data structures that I can share between Ruby and Java, via
> C. I currently have java application and I feel I could code quicker
> certain aspects alot quicker in Ruby if only I could share a data
> structure/object. Ideally I would like to do something like:
>
> ---In My Ruby Code---
> class StringProcessor
> def process_str( str )
> #process
> return process_str
> end
> end
>
> ---In my Java Code----
> class StringProcessor{
> public native String _processString( String str );
> public void processString( String str ){
> this._processString( str );
> }
> public static void main( String[] args ){
> new StringProcessor().processString( aBigString );
> }
> }
>
> -----In my C Code (pseudo)-----
> Java_StringProcessor__processString jstring ( jstring str ){
> char c = jStringToCharArray( str )
> c = Ruby_StringProcessor_processString( c )
> return charToJString( c )
> }
>
> jStringToCharArray char[] ( jstring str ){
> char[] c
> //convert
> return c
> }
>
> charToJString jstring( char[] c )
> jstring j
> //convert
> return j
> }
>
> Ruby_StringProcessor_processString char[] ( rstring str ){
> char[] c;
> //Process
> return c;
> }
>
> This is not a well thought out example. It's just off the top of my head.
>
> So does anyone know if this is possible?
>
> Thanks,
>
> Zach
>
>


Mauricio Fernández

11/17/2004 11:56:00 AM

0

On Wed, Nov 17, 2004 at 03:42:19PM +0900, Zach Dennis wrote:
> This posting is more for a learning thing then anything else at this
> point. I've been playing with Java to C via JNI lately doing some pretty
> basic tests; calling methods, getting and settings fields, passing
> primitive types around, etc...
>
> A while back (a few months ago) I did some Ruby to C tests of pretty
> much the same caliber.
>
> I would like to combine the two approaches and share some objects
> between Ruby to C to Java and vise versa. I am not looking for the JVM
> to be able to process ruby code. (no JRuby)
>
> Has anyone had a similar approach (even if it wasn't Java) where they
> were able to wrap a few Objects and use C as the middle man to communicate?

Take a look at the third issue of rubima (Rubyist Magazine); the Ruby
Library Report introduces rjb and rjni:

http://jp.rubyist.net/magazine...

They both use JNI. rjb is implemented as a C extension, and rjni as a
simple wrapping of JNI (extension) + Ruby code for the higher layers.

You might need http://excite.co.jp...

--
Hassle-free packages for Ruby?
RPA is available from http://www.rubyar...


Florian Gross

11/17/2004 4:56:00 PM

0

Zach Dennis wrote:

> This posting is more for a learning thing then anything else at this
> point. I've been playing with Java to C via JNI lately doing some pretty
> basic tests; calling methods, getting and settings fields, passing
> primitive types around, etc...
>
> A while back (a few months ago) I did some Ruby to C tests of pretty
> much the same caliber.
>
> I would like to combine the two approaches and share some objects
> between Ruby to C to Java and vise versa. I am not looking for the JVM
> to be able to process ruby code. (no JRuby)

There's work being done on the Ruby -> C part. See
http://zenspider.com/~ryand/...

That aside maybe SWIG can be useful for you?

Zach Dennis

11/17/2004 5:04:00 PM

0

Kaspar Schiess wrote:

> Zach Dennis <zdennis@mktec.com> wrote in
> news:419AF224.5070307@mktec.com:
>
>
>>Has anyone had a similar approach (even if it wasn't Java) where they
>>were able to wrap a few Objects and use C as the middle man to
>>communicate?
>
>
> Hello Denis,
>
> I know of rjava (using a tcp connection) and rjb (using a jni native
> interface) as far as ruby to java interfacing goes. I have tested rjb and
> found it to work (with some tweaking) as I wanted it to. I don't know about
> the difficulty of these efforts, but now that you have the source, I guess
> duplicating them is a breeze, depending on how much cheating you allow
> yourself to do.
>

Thanks Kaspar, I will definitely look into rjava and rjb.

Zach


Zach Dennis

11/17/2004 5:05:00 PM

0

Mauricio Fernández wrote:

> On Wed, Nov 17, 2004 at 03:42:19PM +0900, Zach Dennis wrote:
>
>>This posting is more for a learning thing then anything else at this
>>point. I've been playing with Java to C via JNI lately doing some pretty
>>basic tests; calling methods, getting and settings fields, passing
>>primitive types around, etc...
>>
>>A while back (a few months ago) I did some Ruby to C tests of pretty
>>much the same caliber.
>>
>>I would like to combine the two approaches and share some objects
>>between Ruby to C to Java and vise versa. I am not looking for the JVM
>>to be able to process ruby code. (no JRuby)
>>
>>Has anyone had a similar approach (even if it wasn't Java) where they
>>were able to wrap a few Objects and use C as the middle man to communicate?
>
>
> Take a look at the third issue of rubima (Rubyist Magazine); the Ruby
> Library Report introduces rjb and rjni:
>
> http://jp.rubyist.net/magazine...
>
> They both use JNI. rjb is implemented as a C extension, and rjni as a
> simple wrapping of JNI (extension) + Ruby code for the higher layers.
>
> You might need http://excite.co.jp...
>

Thanks for the links Mauricio, I will check these out!

Zach


Zach Dennis

11/17/2004 5:06:00 PM

0

Florian Gross wrote:

> Zach Dennis wrote:
>
>> This posting is more for a learning thing then anything else at this
>> point. I've been playing with Java to C via JNI lately doing some
>> pretty basic tests; calling methods, getting and settings fields,
>> passing primitive types around, etc...
>>
>> A while back (a few months ago) I did some Ruby to C tests of pretty
>> much the same caliber.
>>
>> I would like to combine the two approaches and share some objects
>> between Ruby to C to Java and vise versa. I am not looking for the JVM
>> to be able to process ruby code. (no JRuby)
>
>
> There's work being done on the Ruby -> C part. See
> http://zenspider.com/~ryand/...
>

Thanks for the link, I will look into this.

> That aside maybe SWIG can be useful for you?

I don't know a lot about SWIG, I'll give it a look. Thanks for replying,

Zach


Zach Dennis

11/17/2004 5:07:00 PM

0

Lyndon Samson wrote:

> Sounds like you are better off skipping the C/C++ layer and using
> something like shared memory/memory mapped files to communicate
> between ruby and java.
>
> You could use real threads, or some sort of co-routine.
>
> Simple solution would be to use XML, otherwise you'll need to write
> something that unpacks complex structures from byte arrays.

I will have to do some tests and see speed comparisons. Especially if I
can get Java to generate some YAML files, and have my ruby program read
it in.

>
> BTW, your subject line is a little confusing, I thought the post would
> be about a Ruby to C translator. :-)

Sorry about that. I see what you mean.

Thanks for replying,

Zach


Dido Sevilla

11/17/2004 10:33:00 PM

0

On Wed, 17 Nov 2004 15:42:19 +0900, Zach Dennis <zdennis@mktec.com> wrote:
> Has anyone had a similar approach (even if it wasn't Java) where they
> were able to wrap a few Objects and use C as the middle man to communicate?

Um, why not just use the networking layer of your system and use some
kind of remote procedure call to make your Java and Ruby programs
communicate? That strikes me as far simpler than the scheme that you
seem to be proposing. I don't know if there's a Java package that does
DRb or a Ruby package able to RMI/IIOP, but such a thing might work.
At last resort you may be able to use XML-RPC or even SOAP to get your
programs to do this, but IMHO such contrivances are an abuse of what
XML was designed to do.