Tuesday, June 2, 2009

More refactoring "Refactoring QtRuby Example #1"

In previous post : phosphorescence: Refactoring "Refactoring QtRuby Example #1", I had wrote below :
require 'Qt4'
require 'default/ui'

class Default < Qt::MainWindow
attr_reader :ui

def initialize(parent=nil)
super(parent)
@ui = UI::DefaultClass.new
@ui.setup_ui(self)
yield(self) if block_given?
ObjectSpace.define_finalizer(self, Default.delete_ui(@ui))
end

private
def Default.delete_ui(ui_to_delete)
proc {ui_to_delete.dispose unless ui_to_delete.disposed?}
end
end

But, it's still redundant yet. Ruby's super has following specifications.
  1. if the argument of super are omitted, all the arguments of original method are passed.
  2. super doesn't have to be top of the code block.

Then I refactor code block to below :
require 'Qt4'
require 'default/ui'

class Default < Qt::MainWindow
attr_reader :ui

def initialize(parent=nil)
@ui = UI::DefaultClass.new
super
@ui.setup_ui(self)
ObjectSpace.define_finalizer(self, Default.delete_ui(@ui))
end

private
def Default.delete_ui(ui_to_delete)
proc {ui_to_delete.dispose unless ui_to_delete.disposed?}
end
end

Original post phosphorescence: Refactoring QtRuby Example #1 has been corrected already. Then let's check working correctly.
irb(main):001:0> require 'Qt4'
=> true
irb(main):002:0> require 'default/default'
=> true
irb(main):003:0> Qt::Application.new(ARGV) do
irb(main):004:1* window = Default.new do|w|
irb(main):005:2* puts w.class
irb(main):006:2> puts w.ui.class
irb(main):007:2> end
irb(main):008:1> end
Default
UI::DefaultClass
=> #<Qt::Application:0x00000000c0acb0 objectName="irb">

No comments:

Post a Comment