From: "Hal E. Fulton" Date: 2003-02-12T13:52:37+09:00 Subject: Re: Lexical scope and closures ----- Original Message ----- From: "Patrick Logan" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Tuesday, February 11, 2003 10:21 PM Subject: Lexical scope and closures > I read recently a flaw I did not previously know about... > > "Blocks (closures) in Ruby do not introduce a new scope, only methods > do. > This is generally acknowledged as a flaw by the Ruby community but is > left > in for backwards compatibility. For example: > > x = 0 > [1,2,3].each{|x| print x} > #x now equals 3" I sympathize with you in a way, Patrick, but I do find your choice of language a little strong. I'm not certain it's "generally acknowledged as a flaw" (nor where you are quoting). There is certainly some disagreement as to how some issues ought to be handled. Matz himself has said he made mistakes in this area; I'm only willing to consider something a true design flaw when he says it is. > Of course this is just wrong. I am hoping no one expects an > implementation of Ruby to actually behave this way. It surprised me at first, but I have had more than three years to get used to it. Don't make the mistake of thinking that the list between the bars is like a method's formal parameter list. There are some similarities. But it isn't a formal parameter list, never was, and (I'm guessing) never will be. For example, have you considered the fact that any "assignable" entity can be placed there, for example, a writable attribute? scrollbar = ScrollBar.new(...) (0..100).each {|scrollbar.percent| sleep(0.1) } > Is it generally accepted that programs should not expect this? I would > not be ashamed to break this code in a new implementation of Ruby. As I said, I've gotten used to it. I do *sometimes* depend on this behavior, usually when I want to save a value after a loop terminates normally or abnormally. It's a minor thing, but it saves an extra variable. Of course, it goes without saying that however Matz decides to reconcile all these issues, I will simply learn it and live with it. I suppose we all shall. Hal