Implementing Interface in JRuby 1

Posted by Aaron Feng Fri, 13 Jul 2007 03:01:00 GMT

Recently I decided to convert one of the tools I wrote in C# to JRuby. During the process I needed to implement a Java interface in JRuby. This sounds really weird to me because Ruby has no need for an interface because of the duck-typing system. The syntax for implementing an interface in JRuby is shown below:

class SomeClass
  include javax.swing.tree.TreeCellEditor
  # ...
end

In this case, SomeClass implements TreeCellEditor Java inteface. It uses mix-in like syntax. Prior to the 1.0 release, I believe it used ruby inheritance syntax instead of the current mix-in syntax. That would look even more weird because Ruby doesn't support multiple inheritance and it will be confusing when you are implemneting an interface and extending a class.

I don't know about you, but implementing an interface in Ruby still feels weird to me.

Comments

Leave a response

  1. thilinamb@gmail.com Thu, 22 Nov 2007 04:27:51 GMT

    this did not work for me. i tried to implement java.lang.Throwable interface. dont know why... :(

    buddhika

Comments