Recommended way

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

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 L. [email protected]
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: [email protected] , [email protected]


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email