From: Nicholas Van Weerdenburg Date: 2005-01-21T03:53:13+09:00 Subject: Re: Confused about variable "declarations" On Fri, 21 Jan 2005 03:01:02 +0900, Ilmari Heikkinen wrote: > > On 20.1.2005, at 19:31, Robert Klemme wrote: > > > > > "Mark Hubbart" schrieb im Newsbeitrag > > news:de63abca050120090877cc9971@mail.gmail.com... > >> On Thu, 20 Jan 2005 21:01:01 +0900, Graham Nicholls > >> wrote: > >>> No worries, and thanks, but I think that the problem was that offset > > is > >>> being set inside an iterator (they still confuse me - any pointers to > > a > >>> good explanation would be nice!), which means that it is out of scope > >>> outside the iterator. > >> > >> A short explanation: Blocks have a sort of "leaky scope", where > >> variables from outside the scope can leak in and out, but those that > >> are from the inside don't leak out. for example: > > > > While I completely agree with your explanation the word "leaky" > > strikes me > > as odd as it seems to suggest uncommon or defective behavior: it's > > very > > common in programming languages, that variables are visible in nested > > scopes but not surrounding scopes: > > > > This may be the leaky case: > > x = 10 > arr = (x..100).map{|x| x*2} > x #=> 100 > > :) > > -- > Ilmari Heikkinen > > I'm quite fond of this behaviour. It works nicely in the "optparse" library. e.g. require "optparse" logfile=nil; opts=OptionParser.new do |opt| opt.on('--logfile', '-l', "Specify log file"){|logfile|} end But I was confused initially since I was thinking in C/Java scope terms at first, and then Perl terms at second. But now I'm happy. I do suppose it might be nice to allow seeping (fuzzing didn't sound write) out. That would save some typing, without too much confusion. Nick -- Nicholas Van Weerdenburg