Hi,

At Fri, 1 Feb 2002 23:54:49 +0900,
Niklas Frykholm <r2d2 / acc.umu.se> wrote:
> 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:
(snip)
> What do you think of all this? Is there some other message passing
> mechanism that you would rather see in a ruby GUI?

It looks cool, but Signal is used as POSIX signal.

  module SignalSlot
    def connect(signal, pr = proc)
      ((@_signals ||= {})[signal] ||= []) << pr
      pr
    end

    def disconnect(signal, pr)
      @_signals[signal].delete(pr) if @_signals
    end

    def activate_signal(signal, *args, &pr)
      @_signals[signal].each {|method| method.call(*args, &pr)} if @_signals
    end

    module Sender
      def signal(*args)
        args.each do |a|
          module_eval %Q{def #{a}(*args, &pr) activate_signal(:#{a}, *args, &pr) end}
        end
      end
    end

    def self.append_features(klass)
      super
      klass.extend(Sender)
    end
  end

> 	class Test
	  include SignalSlot

> 	  attr_reader :x
> 	  signal :new_value
> 
> 	  def initialize(x); @x = x; end
> 
> 	  def x=(x)
> 		@x = x
> 		new_value(x)
> 	  end
> 	end
> 
> 	a = Test.new(3)
> 	b = Test.new(7)
> 	a.connect(:new_value) {|i| puts "a value set to #{i}"}
> 	b.connect(:new_value) {|i| puts "b value set to #{i}"}
	a.connect(:new_value, b.method(:x=))

> 	a.x = 5
	a.disconnect(:new_value, b.method(:x=))
> 	a.x = 10

-- 
Nobu Nakada