gutopia-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [rgui-dev] Jumping Right In


From: Tom Sawyer
Subject: Re: [rgui-dev] Jumping Right In
Date: 20 Aug 2002 14:29:53 -0600

hi all,

sorry i've been out of touch. i've been busy with some other things
lately. 

On Thu, 2002-08-15 at 20:02, Massimiliano Mirra wrote:
> Could you point me to the code where you showed this?  I'm afraid I've
> lost myself.  Conceptually, I can't say which method would be better.
> And I'm quite curious about how you implemented an object monitor.

here is the object monitor code i created. this is a slightly old
version of the "finalized" version of this. of course now, we are not
even using this code, instead we are using the new code according to
your (massimiliano's) suggestion of monitoring the variables in and of
themselves. but nonetheless, for the interested:

class Object

  def bind(instance, proc=nil, &block)
    proc = block unless proc
    instance = instance.to_s
    ___automate___ unless @___procs___ and @___instances___
    @___procs___[instance] = proc
  end
  
  def ___automata___ #(*args, &block)
    (self.instance_variables | self.class.class_variables).each { |iv|
      if not ['@___instances___', '@___procs___'].include?(iv)
        current = instance_eval "#{iv}"
        if current != @___instances___[iv]
          @___instances___[iv] = current.dup
          if @___procs___.has_key?(iv)
            @___procs___[iv].call(current) #(*args, &block)
          end
        end
      end
    }
  end
  
  def ___automate___
    $___trigger___ = []  # not sure of best place to put this
    @___procs___ = {} unless @___procs___
    @___instances___ = {} unless @___instances___
    (self.instance_variables | self.class.class_variables).each { |iv|
      if not ['@___instances___', '@___procs___'].include?(iv)
        current = instance_eval "#{iv}"
        @___instances___[iv] = current.dup
      end
    }
    self.methods.each { |meth|
      if not Object.instance_methods(true).include?(meth)
        alias_meth = "___#{meth.hash.to_s.gsub('-', '_')}___"
        if not self.respond_to? alias_meth
          self.instance_eval <<-"EOS"
            class << self
              alias_method :#{alias_meth}, :#{meth}
              def #{meth}(*args, &block)
                return if $___trigger___.include?([self, :#{meth}])
                $___trigger___ << [self, :#{meth}]
                r = send(:#{alias_meth}, *args, &block)
                ___automata___ #(*args, &block)
                $___trigger___.pop
                return r
              end
            end
          EOS
        end
      end
    }
  end
  
end


and here is a dumb test case:


class TestObject

  attr_reader :one
  attr_writer :two
  attr_accessor :three, :four

  def initialize
    @one = 1
    @two = 2
    @three = [ 'john', 'joe' ]
    @four = { 'john' => 36, 'joe' => 34 }
  end

  def alt
    @four.update( { 'tom' => 32 } )
  end

end

testobj = TestObject.new

testobj.bind(:@one) { |x| puts "one is #{x}!" }
testobj.bind(:@two) { |x| puts "two is #{x}!" }
testobj.bind(:@three) { |x| puts "three is #{x}!" }
testobj.bind(:@four) { puts "four's one is #{testobj.one}!" }

testobj.two = 3
testobj.alt






reply via email to

[Prev in Thread] Current Thread [Next in Thread]