matz / zetabits.com (Yukihiro Matsumoto) writes: > Hi, > > In message "[ruby-talk:5483] Re: Some newbye question" > on 00/10/13, Davide Marchignoli <marchign / di.unipi.it> writes: > > |> def example > |> a = 99 > |> return proc { |n| n + a } > |> end > |> > |> x = example > |> x.call(2) # => 101 > > |My question was about variable n, the block does not necessarly introduces > |a fresh variable, instead it (possibly) binds it to an already existing > |variable. IMHO this treatement of variables occurring as argument of > |blocks is unrelated to the creation of closures. > > It is necessary. For example > > fact = proc{|n| > n == 0 ? 1 : fact.call(n-1)*n > } > p fact.call(4) > > would return 0 if the block does not introduce a new scope. > But I still want to remove the stumble stone like: But turning this around, I think the question was why do block parameters inherit variables from the surrounding scope: a = 1 proc { |a| a = 99 } .call a # => 99 Is there a reason why procs should behave differently to methods here? a = 1 def fred(a) a = 99 end fred(12) a # => 1 Dave