Anonymous object's ancestors doesn't list java interfaces?

Hi there,

When I run the following:

module Ex7
  class Task
    include java.lang.Runnable
    def run
      puts "A Java interface method"
  def self.test
    puts Task.ancestors.join(", ")

    task2 =
    class << task2
      include java.lang.Runnable
      def run
        puts "A Java interface method - with anonymous class definition"
    puts task2.class.ancestors.join(", ")

I get the following =>
Ex7::Task, Java::JavaLang::Runnable, JavaProxyMethods, Object, Kernel
Java::JavaLang::Object, ConcreteJavaProxy, JavaProxy, JavaProxyMethods,
Object, Kernel

Shouldn’t the second line include the “Java::JavaLang::Runnable”?


Hmm…does this actually work? I don’t believe it’s possible to << a
Java object and then add interfaces after the fact. What you’re seeing
is perhaps it failing to add Runnable because it can’t implement the

On Wed, Dec 23, 2009 at 3:35 PM, Zemian D. [email protected]


To unsubscribe from this list, please visit:

Hi Charles,

The sample I gave compiles and runs if that what your definition of
“actually work” is.

What I observed is little inconsistency. The output of first line is

“Ex7::Task, Java::JavaLang::Runnable, JavaProxyMethods, Object, Kernel”,
which is from a full Ruby class that included a java interface in it.

But when creating a anonymous class that modify an existing object, the
included java interface does not show, as it display here:

“Java::JavaLang::Object, ConcreteJavaProxy, JavaProxy, JavaProxyMethods,
Object, Kernel”

What’s more strange is “task2.kind_of? java.lang.Runnable” will return
but yet I can’t pass this task2 object into a java method that accepts a
Runnable parameter (like for example).


View this message in context:'s-ancestors-doesn't-list-java-interfaces--tp26907736p26910636.html
Sent from the JRuby - User mailing list archive at

To unsubscribe from this list, please visit:

BTW, you can do this even more simply using JRuby’s auto-magic
block–>interface coercion:

t = { puts “foo” }



You’re adding the interface to the object’s singleton class. This isn’t
currently supported, but there’s no inconsistency. When you write:


you are requesting the ancestors of the main class, in this case
java.lang.Object. To obtain the ancestors of the singleton class, you


which in this case will include java.lang.Runnable.

To create an anonymous Java class, create an anonymous class using Assuming you only need one instance, the
might look something like this:

t = {
include java.lang.Runnable
def run
puts “hi, mom”
}.new #=> instance created right here



Hi Bill, Thanks for the tips, that worked.