From: Dan Muller Date: 2001-03-25T08:40:02+09:00 Subject: [ruby-talk:13181] Re: FEATURE REQUEST: 'my' local variables "Yukihiro Matsumoto" wrote in message news:984665134.285425.2254.nullmailer@ev.netlab.zetabits.com... [text elided] > |Introducing new variables at a beginning of the block is better for > |me. Otherwise I should be more careful of where a variable belonging > |to, since variables can suddenly become block local in a sequence of > |code. > > I can understand you. I also prefer a declarative style. I find the ":=" to be too subtle, too hard to see. Guess I'm getting old; my glasses get stronger every year. :-( I'm not sure if I like the following proposal, but I spent some time thinking about it, so I'll post it rather than toss it... How about a variation on the let idea. In pseudo-BNF: with-expr := 'with' with-body var-init-list with-scope with-scope := block | statement* 'end' with-var-list := '*' | with-var | '(' with-var ( ',' with-var )* ')' )* with-var = simple-var-name ( = expr )? var-list = simple-var-name ( ',' simple-var-name )* The with-expr creates a scope consisting of the following block or list of statements. The variables mentioned in with-var-list are local to that scope. If the with-expr ends with a block, it returns a block. Otherwise it returns the value of the last statement. Using an asterisk in place of a list makes all variables that are first mentioned in the with-expr's scope be local. This is exactly the current behavior of blocks, but this lets you get that behavior for an arbitrary list of statements. The block form of with-expr is important because it lets you make an iterator variable be local, without introducing any syntax between the beginning of the block and the iterator argument list. (I find that introducing something in there is not aesthetically pleasing, and would probably be hard to parse.) a = 1 i = 1 c = 1 (1..3).each with (i,a,b) do |i| a = i b = i c = i end p a # -> 1 p i # -> 1 p b # error p c # -> 3 To force something to have outer scope (e.g. the iterator), you would still need to assign it before the block, but that doesn't seem like a hardship. - Dan Muller