From: Ryan Pavlik Date: 2003-11-26T10:53:19+09:00 Subject: Re: Controlled block variables On Tue, 25 Nov 2003 01:51:36 +0900 "T. Onoma" wrote: > On Monday 24 November 2003 05:22 pm, Jamis Buck wrote: > > Hmm. Couldn't you just do: > > > > a = [ 1, 2, 3 ] > > eval "a" > > In such a simple case, of course. But consider: > > a=[1,2,3] > eval "def z; p a; end; z" > I had a sudden inspiration with respect to this problem. As others have said, you wouldn't expect this to work in any case. However, I believe your ultimate goal is to put together code, so you can do something like: a = [1, 2, 3] eval <<-END def foo(...) : munge(#{a}) : end END It's not really a matter of scope or not being able to put other literals into code---these are just behaviors that prevent you from doing this the way you're currently suggesting. The real question is, what do you _want_? You want to take a predetermined value, and be able to create a function or piece of code that uses this value at some later date. So, the trick is to just do what you want: store it. Here's a quick hack: class Ref @@id = Hash.new def initialize(ob) @@id[ob.__id__] = ob @ob = ob end def to_s return "Ref[#{@ob.__id__}]" end def deref return @ob end def Ref::[](id) return @@id[id] end end # You could even do this: class Object def to_ref return Ref.new(self) end end a = [1, 2, 3].to_ref eval "def foo; p #{a}; end" To say this is a bit of a hack is putting it gently, but it gets the job done, it doesn't rely on redefining the core language, and it only takes a few lines of code. -- Ryan Pavlik "I'm pretty sure that 'Mr Pibb' and 'Dr Pepper' figure rather prominently in this scheme." - 8BT