From: Stephen White Date: 2001-06-02T04:08:54+09:00 Subject: [ruby-talk:16156] Re: Strange idea... exporting from a scope On Sat, 2 Jun 2001, Yukihiro Matsumoto wrote: > Yes. And that's why it has not been merged yet. > I'm still seeking the better way, if any. My previous proposal was turned down on the basis that it broke existing code. Given that upgrading code is possible with a version keyword, it could be worth re-examining the proposal... do/end blocks keep their flat scope, but the parameter list is extended to make the variables shadowed/private. i = 1 1.upto(10) {|i| i = i * 3 } puts i will result in "1". If someone really wanted to define some local variables, they could kludge this by popping a few more variable names in the parameter list. Not nice, but it's easily understood and is not an essential feature. Another example: i = 123 j = 10 (1..10).each do |i| # i becomes shadowed within the block j = i * 5 k = i * 10 end p [i, j, k] -> [123, 50, 100] Next step... Change the other block, {}, to have its own scope with all variables being shadowed/private. Eg: i = 123 j = 10 (1..10).each {|i| # accessing j at this point would be an error j = i * 5 k = i * 10 } p [i, j, k] -> [123, 10, accessing k is an error] Eg, the code in the {}'s is just like it was a separate method. This would also be a useful construct for testing the effect of refactoring a block of code. This would increase the semantic distance between the two block types and make it clearer why there are two kinds of blocks. It could also possibly eliminate the Proc/Method difference by making {}s into syntactic sugar for an anonymous method definition or closure. The <> construct can be used for the existing block definitions. Regardless of whether this proposal makes it or not, I think updating existing code is an interesting approach to the problem of how to upgrade a language without breaking older code. If old code can just be deprecated into clumsier syntax, then the new changes to the language can always have access to the best syntax. -- spwhite@chariot.net.au