This is an excerpt from sample.rb in the ruby-gtk documentation. After
thinking about this and studying the ruby features used here, i still
don't understand _why_ the last three lines are used in this way
(assuming i actually understand what they do . . . which i'm not
entirely sure of, either). Wouldn't including SampleClass (along with
Sample) in the definition of class SampleWindow do the same thing? I
feel like i must be missing something subtle here.
module SampleClass
def invoke
@singleton = new if @singleton.nil? or @singleton.destroyed?
unless @singleton.visible?
@singleton.show_all
else
@singleton.destroy
end
end
end
class SampleWindow < Gtk::Window
include Sample
def initialize(title)
super(Gtk::WINDOW_TOPLEVEL)
set_title(title)
border_width(0)
@destroyed = false
signal_connect("destroy") do destroy end
end
end
class << SampleWindow
include SampleClass
end