On 19.02.2009 18:40, lasitha wrote: > On Thu, Feb 19, 2009 at 9:10 PM, Robert Klemme > <shortcutter / googlemail.com> wrote: >> 2009/2/19 lasitha <lasitha.ranatunga / gmail.com>: >>> Oops, sorry - please ignore the bit about using a Proc. It doesn't >>> solve anything since all it does is parameterize the result and you >>> could have done that with just a regular method. >>> >>> So at this point eval is all i can think of (see Robert's post). >> Still I would consider an ordinary method or a lambda superior to >> using eval. I try to avoid eval whenever possible [...] > > True, thanks for prodding me to think it through again :) > The following is indeed nicer than eval: > > $: irb > 01> x = 1 > --> 1 > 02> s = lambda { "This is #{x}" } > --> #<Proc:0x733930@(irb):2 (lambda)> > 03> s.call > --> "This is 1" > 04> x = 2 > --> 2 > 05> s.call > --> "This is 2" > > Ashikali, the caveat here is that 'x' has to be assigned before the > Proc is created. The initial assignment could be to nil though, the > interpreter just needs it to have seen it. I would prefer an explicit block parameter though - or a method for that matter. It's more encapsulated, i.e. you have a clear interface and do not rely on the rather unobvious closure. Cheers robert