On Fri, Oct 04, 2002 at 11:18:46AM +0900, Alan Chen wrote: > I've asked this before but can't recall any answers. How would one > do the following without non-local block params: > > a = [1.1, 3.14, 2.7, 5.15] > sum = 0.0 > a.each { |elem| sum += a } Shouldn't it be a.each { |elem| sum += elem } Anyway you aren't using (nor need) non-local block params at all! You're simply accessing variables from an external scope, which is OK (how would we get closures otherwise?) and isn't the issue we're to talk about. The thing is after a.each { |elem| sum += elem } elem will be 5.15 iff elem was already in the external scope, otherwise it isn't defined. Compare: a = [1,2,3,4,5] a = [1,2,3,4,5] sum = 0 sum = 0 l = 0 a.each { |l| sum += l } a.each { |l| sum += 1 } # here l == 5 # l is undefined here! With local block parameters, you'd get a = [1,2,3,4,5] sum = 0 l = "sdfsdfs" a.each { |:l| sum += 1 } # I claim this syntax as mine :-) # l is unchanged here! l == "sdfsdfs" > Personally, I like accessing external scope by default. C behaves similiarly: > #include <stdio.h> > > int main() > { > int a = 42; > int b = 55; > > printf("a: %d b: %d\n", a, b ); > > { > int a = 33; /* define a as local scope to this block*/ > printf("a: %d b: %d\n", a, b ); > } > } > > Gives you > a: 42 b: 55 > a: 33 b: 55 > In C you can shadow external vars because you must declare them anyway. In Ruby an assignment to a var creates a new one iff there isn't already one with that name. -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Absolutely nothing should be concluded from these figures except that no conclusion can be drawn from them. -- Joseph L. Brothers, Linux/PowerPC Project)