What is the current syntax for defining a class in JRuby that implements a Java interface?

View: New views
4 Messages — Rating Filter:   Alert me  

What is the current syntax for defining a class in JRuby that implements a Java interface?

by Ken McDonald :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've seen an 'old' way to do it, but suspect there is something more
modern. Is there a page anywhere that gives quick examples of how to do
Java-specific stuff in JRuby?

Thanks,
Ken

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

    http://xircles.codehaus.org/manage_email


RE: What is the current syntax for defining a class in JRuby that implements a Java interface?

by Peter K Chan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ken,
        The new way is to use "include" to include the Java interface, as if
it is a Ruby module.

        For example:

Class RunnableRuby
  include java.lang.Runnable

  def run
    puts :Running
  end
end

Peter

-----Original Message-----
From: Kenneth McDonald [mailto:kenneth.m.mcdonald@...]
Sent: Wednesday, August 15, 2007 11:57 PM
To: user@...
Subject: [jruby-user] What is the current syntax for defining a class in
JRuby that implements a Java interface?

I've seen an 'old' way to do it, but suspect there is something more
modern. Is there a page anywhere that gives quick examples of how to do
Java-specific stuff in JRuby?

Thanks,
Ken

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

    http://xircles.codehaus.org/manage_email



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

    http://xircles.codehaus.org/manage_email


Re: What is the current syntax for defining a class in JRuby that implements a Java interface?

by Ken McDonald :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yup, what I'd seen was definitely _not_ the current way of doing it :-).
In retrospect this is
obvious, but I'd never have guessed it myself :-). Thanks!

Ken


Peter K Chan wrote:

> Ken,
> The new way is to use "include" to include the Java interface, as if
> it is a Ruby module.
>
> For example:
>
> Class RunnableRuby
>   include java.lang.Runnable
>
>   def run
>     puts :Running
>   end
> end
>
> Peter
>
> -----Original Message-----
> From: Kenneth McDonald [mailto:kenneth.m.mcdonald@...]
> Sent: Wednesday, August 15, 2007 11:57 PM
> To: user@...
> Subject: [jruby-user] What is the current syntax for defining a class in
> JRuby that implements a Java interface?
>
> I've seen an 'old' way to do it, but suspect there is something more
> modern. Is there a page anywhere that gives quick examples of how to do
> Java-specific stuff in JRuby?
>
> Thanks,
> Ken
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>  


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

    http://xircles.codehaus.org/manage_email


Re: What is the current syntax for defining a class in JRuby that implements a Java interface?

by Bill Dortch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You can also subclass and implement one or more interfaces at the same time, e.g.

# you probably wouldn't combine these particular
# interfaces,
just an example

class MyFrame < javax.swing.JFrame
  include java.awt.event.ActionListener
  include java.lang.Runnable

  def initialize(*args)
    super
    @button = new javax.swing.JButton 'Click me'
    add @button
    @button.add_action_listener self
   #..etc
  end

  def actionPerformed(event)
  #...
  end

  def run
  #...
  end

end

You can also group interfaces that you frequently use together:

module RunCompare
  include java.lang.Runnable
  include java.lang.Comparable
end

class AClass
  include RunCompare

  def run
  #...
  end

  def compareTo(other)
  #..
  end
end

-Bill

On 8/15/07, Peter K Chan <peter@...> wrote:
Ken,
        The new way is to use "include" to include the Java interface, as if
it is a Ruby module.

        For example:

Class RunnableRuby
  include java.lang.Runnable

  def run
    puts :Running
  end
end

Peter

-----Original Message-----
From: Kenneth McDonald [mailto:kenneth.m.mcdonald@...]
Sent: Wednesday, August 15, 2007 11:57 PM
To: user@...
Subject: [jruby-user] What is the current syntax for defining a class in
JRuby that implements a Java interface?

I've seen an 'old' way to do it, but suspect there is something more
modern. Is there a page anywhere that gives quick examples of how to do
Java-specific stuff in JRuby?

Thanks,
Ken

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

    http://xircles.codehaus.org/manage_email



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

    http://xircles.codehaus.org/manage_email