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(@ui, 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. Look above in detail, attr_reader :ui is set, so it's OK just for yield(self). And 2nd reason, block argument of QtRuby's initializer is limited to one, so yield(@ui, self) is a bug. For these reasons, I refactor code block to 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
Previous 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
NilClass
Default
UI::DefaultClass
=> #<Qt::Application:0x00000000c0acb0 objectName="irb">