From: Niklas Frykholm Date: 2002-02-02T02:54:58+09:00 Subject: Re: [GUI] Signal/slot pattern in ruby [nobu.nokada@softhome.net]: >> I think the signal/slot mechanism in Qt is the most elegant basic >> mechanism. (The other options are AFAIK: callbacks, observers and >> subclassing.) In a ruby GUI, signals and slots should be ordinary ruby >> functions. It should also be possible to use Proc's as slots. Like >> this: > > It looks cool, but Signal is used as POSIX signal. (snip) I like most of your changes. Neat trick using append_features. But I like to have the option to write a.connect(:new_value, b, :x=) As well as a.connect(:new_value, b.method(:x=)) since the former is more symmetric. Here is an implementation that allows both: module SignalSlot def connect(signal, *args, &pr) ((@_signals ||= {})[signal] ||= []) << (pr ? [pr] : args) end def disconnect(signal, *args) @_signals[signal].delete(args) if @_signals end def activate_signal(signal, *args, &pr) @_signals[signal].each {|obj, *meth| obj.send(*((meth[0] ? meth : [:call])+args), &pr) } if @_signals end def self.append_features(klass) super class << klass def signal(*args) args.each {|a| module_eval %Q{def #{a}(*args, &pr) activate_signal(:#{a}, *args, &pr) end} } end end end end It also lets you add arguments to the methods, like this: def print_value(name, v) puts "#{name} value set to #{v}" end a.connect(:new_value, self, :print_value, 'a') I'm not sure what to do with this module. Should I put it in the RAA? Seems a bit silly for such a short module. I guess that is the problem with ruby. Something that requires umpteen lines of code and a special preprocessor (moc) in C++ can be done in 28 lines of ruby. Oh well... // Niklas