From: "David A. Black" Date: 2008-03-14T22:15:59+09:00 Subject: Re: Closures 101 Hi -- On Fri, 14 Mar 2008, jeffperforce@gmail.com wrote: > class Object > def singleton_class > class << self > self > end > end > end > > data = Object.new > status = "good" > data.singleton_class.class_eval { > define_method(:status) { > status > } > } > p data.status # => "good" > status = "bad" > p data.status # => "bad" > > I have a decent amount of experience with ruby, but I have somehow > misunderstood closures this whole time. I thought a closure contained > a full "snapshot" of the stack, but actually it contains references to > the stack. That is, I thought <> would have no > effect. I think it's best to think of the closure as preserving a binding. In that binding, the identifier "status" is bound to "bad". If you reassign to status, you're changing the binding of status inside the (overall) binding. The closure continues to reflect that binding. David -- Upcoming Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS, April 14-17 2008, New York City CORE RAILS, June 24-27 2008, London (Skills Matter) See http://www.rubypal.com for details. Berlin dates coming soon!