On Sun, Aug 04, 2002 at 10:32:44PM +0900, Harry Ohlsen wrote:
> > >    def instance_var_created(c, name, value)
> > >       $stderr.puts "@#{name} of class #{c.class} set to #{value}"
> > >    end
> > >
> > > Is this possible?

Would it be enough for you to catch creation of instance variables in
each method before it returns, to do what you want with them
(e.g. swapping their value with a wrapper of the value)?

If so, try the following.  Beware, hacking into that class_eval is
quoting hell, as there are two levels of weak quotes.


module MonitorVariables
  def self.append_features(klass)
    klass.instance_methods.each do |method|

      klass.class_eval <<-EOF
      alias ___#{method} #{method}
      def #{method}(*params,&block)
        old_instance_variables = instance_variables

        ___#{method}(*params,&block)

        diff = instance_variables - old_instance_variables
        diff.empty? or puts "variable(s) " + diff.inspect + " added in method #{method}"
      end
      EOF

    end
  end
end


class Test
  def foo 
    @hello = 1
  end

  def bar 
    # no var added
  end

  include MonitorVariables
end

t = Test.new

t.foo
t.bar


I guess the thing could be refactored into append_code_to_method() and
on_variable_added_event()...


Massimiliano