From: Massimiliano Mirra Date: 2002-08-05T05:51:40+09:00 Subject: Re: Detecting when an instance variable is created/set On Mon, Aug 05, 2002 at 04:59:40AM +0900, Harry Ohlsen wrote: > > 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)? > > Unfortunately, for it to be useful to me, I'd specifically need it to work in > initialize() as well. I don't know why it does not work in initialize (I assumed it would). A dirty fix: module Initializer def initialize(*args) puts "Here I'll handle these variables: #{instance_variables.inspect}" end end module MonitorVariables ... end class Test def initialize(*args) @a = 1 @b = 'world' super(*args) end ... include MonitorVariables, Initializer end t = Test.new -> Here I'll handle these variables: ["@a", "@b"] I also don't know why, if put in MonitorVariables, initialize won't be called, and needs to be put in a different module. > > If so, try the following. Beware, hacking into that class_eval is > > quoting hell, as there are two levels of weak quotes. > > > > Wow! By the time I feel comfortable that I completely understand that block > of code, I'll be able to put "Ruby guru" on my resume! You obviously already > have it on yours :-). Nah, just a tad more than a rookie, really. Massimiliano