Fitzgerald,Greg said: > > I'm trying to make a function act as a counter, but I can't seem to get > it to work. Here's what I've tried: [... example elided ...] > Isn't the idea of a closure that everything in the block is executed in > the same scope it was defined? It seems that the name 'counter' is > referenced under the name 'count', and so when I redefine 'count', the > 'counter' variable gets lost. Anyone know what's really happening here? 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). The traditional way to approach this is ... def make_counter counter = 0 proc { counter += 1 } end c = make_counter c.call # => 1 c.call # => 2 But that doesn't make a function look like a counter as you were attempting. -- -- 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)