From: Amos Date: 2001-03-18T06:16:43+09:00 Subject: [ruby-talk:12789] scope stuff This morning while I was still lying in bed, I was trying to see if my dense mind could recall the various issues regarding this local scope stuff, a thread I've only been partially able to follow due to limited time. This is how things currently work, right? b = false i = 0 5.times {|i| print i, " " } print "\n", i, "\n" print b, "\n" -> 0 1 2 3 4 -> 4 -> false Going over things out load, the symbols between the bars (|) are not local to that block, which can frustrate folks because of the side effects that can have. Must admit, that aspect of blocks wasn't immediately apparent to me when I was first going through the pickaxe book. In my simplistic mind, I just saw the items between the bars as being the same thing as function arguments. Silly me! Okay, so what if the symbols between the bars were pretty much the same as function arguments? That is, like with functions arguments, the symbols between the bars were, at least by default, local to that scope. Then perhaps the following might be possible? b = false i = 0 5.times {|i| print i, " " } print "\n", i, "\n" print b, "\n" -> 0 1 2 3 4 -> 0 -> false b = false i = 0 5.times {|::i| print i, " " } print "\n", i, "\n" print b, "\n" -> 0 1 2 3 4 -> 4 -> false b = false i = 0 5.times {|::i, b=true| print i, " " if b } print "\n", i, "\n" print b, "\n" -> 0 1 2 3 4 -> 4 -> false In the second chunk of code the scope operator (::) is used to designate that the following symbol is to be bound to the calling scope, if that makes any sense. In the third block, the second argument between the bars is assigned a default value. The idea behind that is so that the invocation of yield won't blow up because of the wrong number of arguments, yet it allows one to define a symbol to have local scope. Well, such as it is, that's what my mind spit out this morning. -- Amos