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

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

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

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

···

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

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

http://jp.rubyist.net/magazine/?0003-RLR

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 Excite エキサイト

···

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?

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

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/Ruby2C.pdf

That aside maybe SWIG can be useful for you?

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.

···

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?

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

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/?0003-RLR

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 Excite エキサイト

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

Zach

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/Ruby2C.pdf

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

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

Sorry about that. I see what you mean.

Thanks for replying,

Zach