From: adamon@... (Damon) Date: 2002-06-13T02:02:24+09:00 Subject: Re: ruby-dev summary 17252-17356 Dave Thomas wrote in message > > In that case, what does the semicolon mean? > > It separates parameters from variables which are local to the block. > > > a = 1 > i = 2 > (1..10).each do |i| > a = i*i > end > > p a #=> 100 > p i #=> 10 > > .. > > a = 1 > i = 2 > (1..10).each do |i;a| > a = i*i > end > > p a #=> 1 > p i #=> 10 > > and I assume > > a = 1 > i = 2 > (1..10).each do > a = i*i > end > > p a #=> 1 > p i #=> 2 > How about: a = 1 i = 2 (1..10).each with{a} do |i| a = i*i end p a #=> 100 p i #=> 2 a = 1 i = 2 (1..10).each with{a,i} do |i| a = i*i end p a #=> 100 p i #=> 10 a = 1 i = 2 (1..10).each do |i| a = i*i end p a #=> 1 p i #=> 2 a = 1 i = 2 (1..10).each do with{i} |i| a = i*i end p a #=> 1 p i #=> 10 That is: Modifications to a block variable are visible outside of the block only if explicitly specified using a 'with' cause. To put it another way, variables are passed into the block "by value". Only if specified using a 'with' clause are they passed in "by reference". If a variable is passed in "by value" then any changes made to that variable are restricted to the block; thus we get shadowing for free. Damon