Forum: JRuby recommended way

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ef31027f8a39cb4b822094019a884849?d=identicon&s=25 Jean Lazarou (Guest)
on 2009-02-28 10:28
(Received via mailing list)
I am using some layout manager implemented in Java, say FlowLayout.

I need to add methods used only at window creation time, I can either do
something like

  class MyLayout < java.awt.FlowLayout
    def setOption x
      ...
    end
  end

  layout = MyLayout.new

or I can do

  layout = java.awt.FlowLayout.new

  def layout.setOption x
    ...
  end

(JVM won't be calling the new method)

If I don't care about adding 'singleton classes', are they equivalent?
Is one recommended? Are they adding the same overhead? Is there a better
way for JRuby?

-Jean

Here is a complete example

x----------------------------------------------------------------------------x

require 'java'

extend_class = false

if extend_class

  puts "extending class"

  class MyLayout < java.awt.FlowLayout
    def set_options v, h
      setVgap v
      setHgap h
    end
  end

  lay_mgr = MyLayout.new

else

  puts "don't extend class"

  lay_mgr = java.awt.FlowLayout.new

  def lay_mgr.set_options v, h
    setVgap v
    setHgap h
  end

end

lay_mgr.set_options 10, 40

frame = javax.swing.JFrame.new

frame.layout = lay_mgr

frame.add javax.swing.JButton.new('Hello')
frame.add javax.swing.JButton.new('World')

frame.visible = true
frame.pack
frame.default_close_operation = javax.swing.JFrame::EXIT_ON_CLOSE

x----------- end of file
--------------------------------------------------------x




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

    http://xircles.codehaus.org/manage_email
13313ac2ec7ba7c43b1b952db034ff3b?d=identicon&s=25 Thomas E Enebo (Guest)
on 2009-02-28 16:46
(Received via mailing list)
I think it depends on whether you have aspirations on expanding your
script in the future.  Having it as a real class means you can nicely
document it and spec/test it.  Attaching a method to a single instance
seems more like a one-off use.   If it is one-off than whatever
strikes your fancy.  If not then you might as well build it up.  Plus,
if you do ever want to override a Java method the class form is nicer.

-Tom

On Sat, Feb 28, 2009 at 3:27 AM, Jean Lazarou <jeanlazarou@yahoo.com>
wrote:
>  layout = MyLayout.new
>
> extend_class = false
>  end
>    setVgap v
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: enebo@acm.org , tom.enebo@gmail.com

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

    http://xircles.codehaus.org/manage_email
This topic is locked and can not be replied to.