From: "T. Onoma" Date: 2003-11-28T09:38:46+09:00 Subject: Re: Controlled block variables On Wednesday 26 November 2003 12:05 pm, Pit Capitain wrote: > maybe you can make use of "define_method" as in > > class X > def self.add_method name, value > eval "define_method name, proc{value}" > end > end > > X.add_method :array, [1, 2, 3] > X.add_method :hash, {1=>"A", 2=>"B"} > > x = X.new > > p x.array # => [1, 2, 3] > p x.hash # => {1=>"A", 2=>"B"} I'll respond here to keep core as prestine as some seem to want it to be. Which is fine but indicates to me that we could use a second list for such things. Anyway. Sorry I let this slip by, I wanted to think on it more. And though I have, I must admit I'm not quite seeing it --quite possibly my small brain just isn't cutting it :-) But perhaps too, there's a communication disconnect. So let me lay down the core problem and you show me how your solution solves it. That should tell us if were on the same page. a = [1,2,3] class_eval { def ameth p a end } So the problem is, a will be eval'd as nil, since it is outside of def. To overcome I must convert a to a literal and stick it in a string. a = [1,2,3] a = "[#{a.join(',')}]" class_eval %Q{ def ameth p #{a} end } So how does you solution deal with this problem? For comparison I will point out Ryan's solution. Ryan created an object pool that has a global scope, then created a special method in the Object class that adds any object to the pool, so one then does something like: a = [1,2,3] a.to_pool class_eval { def ameth p Pool.a end } Of course, there is also the solution: a = [1,2,3] $a = a class_eval { def ameth p $a end } While I like Ryan's solutions b/c it just seems like a useful thing to have an object pool handy without resorting to global variables, one can easily say these are hacks. And for the problem at hand there isn't actually a solution that isn't. But it is certainly interesting to see who can come up with the best one. -t0