From: "Mauricio Fernández" Date: 2002-10-04T16:59:40+09:00 Subject: Re: Specifying local and external block parameters (that old chestnut) On Fri, Oct 04, 2002 at 11:38:46AM +0900, Hal E. Fulton wrote: > ----- Original Message ----- > From: "Gavin Sinclair" > > Thus, if 'a' and 'c' are regular parameters and 'b' is an external variable, > > then one could write > > > > collection.each do |a,:b,c| > > ... > > end See my note on this syntax in my original reply to Gavin :-) > > One problem is that it's not just a simple variable. It > can be anything that responds to assignment. For example, > an accessor: > > 0.upto(99) {|frame.scroll| sleep 0.1} This works unchanged if you accept ':' as a local scope indicator, only to be used on variables (not on accesor methods, as it'd be meaningless), so Ruby would choke on 0.upto(99) {|:frame.scroll| sleep 0.1} as anyway what you really want is 0.upto(99) {|frame.scroll| sleep 0.1} > > I'd like to put a question to the audience :) Does anyone actually have a > need > > to use non-local block parameters? Ever? > > The above is an example. > > Also what if I want the last value(s) of a loop? > > x,i = nil > myobj.each_with_index do |x,i| > # process... > break if some_condition > # more processing... > end > > last_obj = x > last_index = i > > Yes, I *could* save them off in different variables > right before I did the break: > > y,j = nil > myobj.each do |x,i| > #... > if some_condition > y,j = x,i > break > end > #... > end > > last_obj, last_index = y,j > > Two more variables and much ugliness. No, thanks. One more reason to use ':' to indicate local, so the former code runs unchanged. -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Not only Guinness - Linux is good for you, too. -- Banzai on IRC