From: Mike Gold Date: 2008-09-30T16:49:19+09:00 Subject: Re: ruby1.9 block scope Yukihiro Matsumoto wrote: > > As Nobu stated, you can explicitly declare block local variables, > using ';'. Besides that above code would not share variable x, since > the variable is not used upper level. They are two distinct > variables at the same level, with a same name. See the following > code, that makes a variables shared among blocks: > > class Foo > defined_method :bar do > x = 1 > end > defined_method :baz do > x = 2 > end > x = 45 # this assignment would make x shared with foo and bar under > the new rule. > end > > The reason I haven't introduced it yet in Ruby is that single > assignment after the blocks can change the scope of a variable > afterward. Same thing happens assignments _before_ the blocks > already. But I hesitated to enhance that far. > > matz. Making the scope of x contingent upon what comes later would result, I believe, in extreme confusion. Let's say Foo is a large class. Joe Schmo, who did not write Foo, comes along and adds some code to the bottom, such as the 'x = 45' in your example. Suddenly there is a bug in Foo. It seems impossible to Joe, but there it is. He wonders if it is caused by the recent sunspots. He wraps his computer in tin foil, but to no avail. The tests for bar and baz still fail as a result of his (obviously, to him) unrelated change. This was the 'tall' case, but there is also the 'wide' case of deeply nested scopes. An equal helping of confusion applies here too. One might argue that when 'x = 45' appears before bar and baz, this _already_ changes the meaning of bar and baz. But the situation here is entirely different, in my mind. Don't most of us read code from top to bottom? -- Posted via http://www.ruby-forum.com/.