Ruby Forum JRuby > Calling Ruby ".class" from Java

Posted by leandrodoze (Guest)
on 13.08.2008 19:07
(Received via mailing list)
Hello!

I have a compiled (with jrubyc) Ruby class in classpath of my Java
application (on Eclipse), and I can see this class on my Java code.

*** RUBY CLASS ***

class Cachorro
  def diga_ola
    puts 'Ola!'
  end
end

*** JAVAP ***

>javap Cachorro

Compiled from "Cachorro.rb"

public class Cachorro extends org.jruby.ast.executable.AbstractScript{
    public Cachorro();

    public static {};

    public org.jruby.runtime.builtin.IRubyObject
__file__(org.jruby.runtime.Thre
adContext, org.jruby.runtime.builtin.IRubyObject, 
org.jruby.runtime.Block);

    public org.jruby.runtime.builtin.IRubyObject
class_0$RUBY$Cachorro(org.jruby
.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject,
org.jruby.runtime
.Block);

    public org.jruby.runtime.builtin.IRubyObject
method__1$RUBY$diga_ola(org.jru
by.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject,
org.jruby.runti
me.Block);

    public org.jruby.runtime.builtin.IRubyObject
method__1$RUBY$diga_ola(org.jru
by.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject,
org.jruby.runti
me.builtin.IRubyObject[], org.jruby.runtime.Block);

    public org.jruby.runtime.builtin.IRubyObject
class_0$RUBY$Cachorro(org.jruby
.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject,
org.jruby.runtime
.builtin.IRubyObject[], org.jruby.runtime.Block);

    public org.jruby.runtime.builtin.IRubyObject
__file__(org.jruby.runtime.Thre
adContext, org.jruby.runtime.builtin.IRubyObject,
org.jruby.runtime.builtin.IRub
yObject[], org.jruby.runtime.Block);

    public org.jruby.runtime.builtin.IRubyObject
load(org.jruby.runtime.ThreadCo
ntext, org.jruby.runtime.builtin.IRubyObject,
org.jruby.runtime.builtin.IRubyObj
ect[], org.jruby.runtime.Block);

    public static void main(java.lang.String[]);
}

*** JAVA CLASS ***

public class Adestrador {
    public static void main(String[] args) {
        Cachorro c = new Cachorro();
    }
}


This code worked without problem.

But, if I do call c.diga_oi(), I get a exception, because diga_oi() is 
not
exist.

So, the method method__1$RUBY$diga_ola already exist, but diga_ola no.

What the problem?

Somebody can help me?

Thanks so much!

---
My files:

http://www.nabble.com/file/p18967254/Adestrador.java Adestrador.java
http://www.nabble.com/file/p18967254/Cachorro.rb Cachorro.rb
http://www.nabble.com/file/p18967254/Cachorro.class Cachorro.class
---
My environment:

* JDK 1.5.0_13
* JRuby 1.1.3
---
--
View this message in context: 
http://www.nabble.com/Calling-Ruby-%22.class%22-from-Java-tp18967254p18967254.html
Sent from the JRuby - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Posted by Charles Oliver Nutter (Guest)
on 13.08.2008 21:25
(Received via mailing list)
leandrodoze wrote:
> 
> Thanks so much!

The code compiled by jrubyc is not a "normal" Java class. Because Ruby
is very dynamic, and because it constructs all classes at runtime, the
current compiler basically just turns a .rb file into a .class file with
a bunch of method bodies in it. This is not a Java class you can
instantiate and call methods on directly from Java. That feature is
planned for a future compiler, but does not exist yet.

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Posted by leandrodoze (Guest)
on 13.08.2008 22:41
(Received via mailing list)
Charles Oliver Nutter-2 wrote:
> 
> The code compiled by jrubyc is not a "normal" Java class. Because Ruby 
> is very dynamic, and because it constructs all classes at runtime, the 
> current compiler basically just turns a .rb file into a .class file with 
> a bunch of method bodies in it. This is not a Java class you can 
> instantiate and call methods on directly from Java. That feature is 
> planned for a future compiler, but does not exist yet.
> 
> - Charlie
> 

Do you already seen this example?

http://pastie.org/pastes/164540 http://pastie.org/pastes/164540

He calls the method bar() normally as a Java method.

I took it as a base.

Thank you!

- Leandro
--
View this message in context: 
http://www.nabble.com/Calling-Ruby-%22.class%22-from-Java-tp18967254p18970811.html
Sent from the JRuby - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Posted by Peter K Chan (Guest)
on 13.08.2008 23:28
(Received via mailing list)
He did, and his code does not work. Note how his compiled Ruby class 
also does
not contain bar(), since the JRuby compiler is not meant to be compiling 
Ruby
code for call from Java.

Peter
Posted by leandrodoze (Guest)
on 13.08.2008 23:49
(Received via mailing list)
Peter K Chan wrote:
> 
> He did, and his code does not work. Note how his compiled Ruby class also
> does
> not contain bar(), since the JRuby compiler is not meant to be compiling
> Ruby
> code for call from Java.
> 
> Peter
> 

Hello Peter!

Yes, but I thought it worked. =(

I understood by the Charle's post which the goal of jrubc for now.


Charles Oliver Nutter-2 wrote:
> 
> This is not a Java class you can instantiate and call methods on directly
> from Java.That feature is planned for a future compiler, but does not
> exist yet.
> 

Anyway, thank you so much for response!

By the way, how do you calls JRuby methods from the Java code?

This is only way?

http://wiki.jruby.org/wiki/Java_Integration
http://wiki.jruby.org/wiki/Java_Integration

Again, thank you!
--
View this message in context: 
http://www.nabble.com/Calling-Ruby-%22.class%22-from-Java-tp18967254p18971873.html
Sent from the JRuby - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email