From: "Guy N. Hurst" Date: 2000-10-15T23:40:26+09:00 Subject: [ruby-talk:5548] Re: Some newbye question ts wrote: > > >>>>> "G" == Guy N Hurst writes: > > G> Or if there are conflicts, maybe '%' would be better. > > pigeon% ruby -e '%a' > -e:1: unknown type of %string > pigeon% > Actually, I realized later that using '%' would conflict with the %string constructs such as %w( ), %x( ), etc. I have another idea, though. Let me recall matz: > |> (a) I don't like variable overriding, especially in declaration-less > |> language like Ruby. > |> > |> (b) since blocks were introduced for iteration originally, I want > |> any assignables can be block parameters. > > 3 possible solution in my mind for the future: > > (1) local variables in the block parameters be valid only in the > surrounding block. I think Davide proposed this one. This > violates (a), and introduces incompatibility (small?). This is > not perfect since the problem remains for non parameter local > variables. > So, to offer more thoughts for those interested, I would like to reconcile (1) to (a) and (b) - within the limits of my understanding, that is. - certain local variables in the block parameters can be restricted to block (1) - regular local variable act as usual (b) A simple syntactical sugar(?) (such as a trailing tilde '~') could be used to denote new block variables that get initialized to the variable of its namesake. (e.g. |x~| would mean create a new block variable x with default value same as x if local variable x exists, whereas |x| retains current meaning). It would say to the average person "don't keep changes to this var after the block ends". Example: i = 2; iterator(5) {|i| i += 1 } p i -> 7 i = 2; iterator(5) {|i~| i += 1 } p i -> 2 p i~ -> not defined They could be mixed: i = 2; x = "abc"; iterator(5) {|i~,x| i+=1; x="xyz"} p i -> 2 p x -> "xyz" They can't be used for non-parameters, but that is not really so bad, since the original behavior can be kept: i = 2; x = "abc"; iterator(5) {|i~,x,r~| i+=1; x="xyz"; q="keep"; r="pitch"} p i -> 2 p x -> "xyz" p q -> "keep" p r -> undefined They should handle nesting: (ts mentioned nesting before I sent this out...) a = 24 # local variable iterator(5){|a~, b| b # dynamic variable created a # here a is new block variable with value 24 b = 12 block{|a~,b~| b # block variable, has value 12 a # block variable, has value 24 b = 24 a = 36 } # block scope ends; b # here b has the value 12 again a # here a has value 24 again a = 5 } a # local variable has value 24 again b # has value 12 (existing behavior) Could this be implemented? I have an interesting idea about it, but I am no compiler programmer. I will wait for some responses before I mention more. Meanwhile, I need to get some sleep. Guy N. Hurst -- HurstLinks Web Development http://www.hurstlinks.com/ Norfolk, VA - (757)623-9688 PHP/MySQL - Ruby/Perl - HTML/Javascript