Converting Ruby class to Java class

Hi all,
I’m wondering is there a way to convert a ruby class to java class.

Think of the following code

Class X; end
x = X.new
x.class # Gives me the ruby Class for this object

Now I have a java method which accepts java.lang.Class objects, how can
convert x.class to java.lang.Class

Thanks,

Cheers,

-A


Ali S., Research Assistant, LSIR - Distributed Information Systems
Laboratory
EPFL-IC-IIF-LSIR, Bâtiment BC, Station 14, CH-1015 Lausanne, Switzerland.
http://lsirpeople.epfl.ch/salehi/
email: [email protected]
Tel: +41-21-693.6656/7563 Mobile: +41-21-78.815.2020 Fax:
+41-21-693.8115


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Ali,

In some cases you could use #java_class method, but it only works for
those classes that originate from Java side, like this:

require ‘java’

MyString = java.lang.String
p MyString.java_class # => java.lang.String
p MyString.java_class.class #==> Java::JavaClass

Thanks,
–Vladimir

On Tue, Jul 29, 2008 at 2:57 PM, Ali S. [email protected] wrote:

convert x.class to java.lang.Class
http://lsirpeople.epfl.ch/salehi/


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Wiki to the rescue! [1]

x.java_class

[1]
http://wiki.jruby.org/wiki/Calling_Java_from_JRuby#Referencing_a_java.lang.Class_object

This is a sample program trying to us an object created in JRuby inside
a java program which doesn’t work.

public static void main(String[] args) throws Exception {
org.jruby.Main.main(new String[] {“some_code.rb”});
Object w = Class.forName(“RubyTest2”).newInstance();
}

and inside “some_code.rb” file:

class RubyTest2;end

In this example I simply want to use an object created in ruby inside a
java program.

I appreciate your comments on this sample code.

Cheers,
-A

Vladimir S. wrote:

Class X; end
-A
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email


Cheers,

-A


Ali S., Research Assistant, LSIR - Distributed Information Systems
Laboratory
EPFL-IC-IIF-LSIR, Bâtiment BC, Station 14, CH-1015 Lausanne, Switzerland.
http://lsirpeople.epfl.ch/salehi/
email: [email protected]
Tel: +41-21-693.6656/7563 Mobile: +41-21-78.815.2020 Fax:
+41-21-693.8115


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Just for others, to get access to java.lang.Class object of a given ruby
object, one has to create his ruby object by extending java.lang.Object

Class X; end
x = X.new
x.java_class # Error, method not found

FIX:

Class X<<Java::java.lang::Object ; end
x = X.new
x.java_class # Error, method not found

-A

Vladimir S. wrote:

Class X; end
-A
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email


Cheers,

-A


Ali S., Research Assistant, LSIR - Distributed Information Systems
Laboratory
EPFL-IC-IIF-LSIR, Bâtiment BC, Station 14, CH-1015 Lausanne, Switzerland.
http://lsirpeople.epfl.ch/salehi/
email: [email protected]
Tel: +41-21-693.6656/7563 Mobile: +41-21-78.815.2020 Fax:
+41-21-693.8115


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

And is there any plans to improve this in the next releases (e.g., 1.2)
?

-A

Charles Oliver N. wrote:

  • Ruby classes are “open”, which means you can add new methods to
    pass those objects back.

  • Charlie


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Cheers,

-A


Ali S., Research Assistant, LSIR - Distributed Information Systems
Laboratory
EPFL-IC-IIF-LSIR, Bâtiment BC, Station 14, CH-1015 Lausanne,
Switzerland.
http://lsirpeople.epfl.ch/salehi/
email: [email protected]
Tel: +41-21-693.6656/7563 Mobile: +41-21-78.815.2020 Fax:
+41-21-693.8115


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Yes, a compiler which produces “normal” classes you can instantiate is
planned for an upcoming release. Currently we’re in the process of
cleaning up and refactoring the existing Java integration layer in
preparation for that move.

Ali S. wrote:

org.jruby.Main.main(new String[] {“some_code.rb”}); Object w =
In general Ruby classes do not have a java.lang.Class associated with
command-line compiler that “freezes” a class definition it sees in a

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Ali S. wrote:

class RubyTest2;end

In this example I simply want to use an object created in ruby inside a
java program.

In general Ruby classes do not have a java.lang.Class associated with
them. Why is this? Several reasons:

  • Ruby classes are created at runtime, so even if they had a Class you
    wouldn’t be able to reference them until they’d been loaded
  • Ruby classes are “open”, which means you can add new methods to them.
    Java classes, represented by java.lang.Class, are unmodifiable once
    loaded.

It is theoretically possible to create a normal Ruby class that looks
and acts like a Java class, but only with an as-yet-unwritten
command-line compiler that “freezes” a class definition it sees in a
file and turns it into a full-on Java class.

In general your best bet for creating Ruby objects that conform to a
specific class or interface is to extend or implement from Ruby and pass
those objects back.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email