From: James Edward Gray II Date: 2005-11-20T06:53:19+09:00 Subject: Re: Cond loop vs iterator and scope questions On Nov 19, 2005, at 3:47 PM, Todd wrote: > OK, maybe I'm going about it the wrong way. I specified it wrong in > the original post anyway. What I want to do is have the block be able > to modify the variables in the method that's yielding to it. > > Like: > > def foo > a = 1 > yield > #block does something with a, such that this a is now a new value, > say add 1 > a > end > > foo { #some code here } > > => 2 Use block parameters for this: >> def foo >> a = 1 >> a = yield a >> p a >> end => nil >> foo do |var| var + 1 end 2 => nil > After looking at your code, I think I can accomplish this more > elegantly (by inheriting from Array). It can be tricky, inheriting from Ruby's core classes and thus is probably better avoided. Favor composition over inheritance. James Edward Gray II