From: Charles Oliver Nutter Date: 2009-06-11T02:35:28+09:00 Subject: Re: JRuby: Instance/Object method ovveride on a JPanel fails? On Wed, Jun 10, 2009 at 12:20 PM, Curator1 wrote: > I like when learning a new language to play around with how to do > things.  So in one case I did the two standard java idioms in JRuby to > display an image [methods dispaly and display2], and then decided to > try the Ruby idiom of modifying an object on the fly and over-ride > it's paintComponent(g) method as shown below [method display3].  I am > pretty sure I got the idiom right, so am I bumping up against a > limitation of JRuby or have I botched the invocation or is it a > bug.... ... >   def display3(image) #using anonymous instance >     x=image.getWidth >     y=image.getHeight >     frame=JFrame.new("Quick Display 3") >     frame.setSize(x,y) >     panel=javax.swing.JPanel.new > #let's do an instance over-ride. >     class << panel ... > import javax.imageio.ImageIO > #debugger will need to insert their own jpg file below > img = ImageIO.read(java.io.File.new("G:\\wildpics\ > \crystal_face.jpg")); > u=TinoGraphics::Utility.new > u.display2(img) #works as expected! > u.display3(img) #does not work :( Yes, this is expected. In the display2 case you're actually extending a Java class, so you gain the ability to override whatever methods you like. In the display3 case, the Java inheritance/dispatch semantics are already set in stone, and we can't force Java to dispatch to Ruby from already-bound Java code. Your best options would be to just create another subclass or create a single subclass for which you can provide different logic on a per-object basis (like a version that delegates to an object of your choice, and then you just provide it on construct or set it into an attribute. - Charlie