From: Rick DeNatale Date: 2006-09-12T10:39:04+09:00 Subject: Re: Struggling with Blocks On 9/11/06, Morton Goldberg wrote: > On Sep 11, 2006, at 1:55 PM, Rick DeNatale wrote: > > > So, what's the real deal with BEGIN..END. What it really does is to > > delimit a series of statements which are treated like an anonymous > > method which is called in place. > > That's what I thought until recently, but there is a difference. > Consider: > > > a = 1 > begin > a = 2 > b = 3 > p [a, b] # => [2, 3] > end > p a # => 2 > p b # => 3 > > > If the begin ... end behaved _exactly_ like an anonymous method, we > would see: > > > a = 1 > begin > a = 2 > b = 3 > p [a, b] # => [2, 3] > end > p a # => 1 > p b # => raises NameError > > > Frankly, I would be happier with the latter behavior. I have to admit that I was struggling a bit when I made the analogy to an anonymous method. The key is that begin pushes a new stack frame onto the call stack. You are right about the scoping of local variables. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/