From: Jim Weirich Date: 2004-08-04T04:37:27+09:00 Subject: Re: Closure question Jim Weirich said: > The proc (i.e. closure) doesn't get defined until the second time count is > called. By that time, the counter variable is long out of scope (the body > of a "def" does not form a closure, so the counter variable is not in > scope when the proc is evaluated). So the obvious answer is: Don't use a "def" to redefine count. Use "define_method" which takes a closure/block as the method body. def count counter = 1 self.class.send(:define_method, :count) { counter += 1 } counter end p count #=> 1 p count #=> 2 p count #=> 3 -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)