From: "ara.t.howard" Date: 2008-11-01T14:40:56+09:00 Subject: Re: How to access to local variables in enclosing scopes? On Oct 31, 2008, at 3:08 PM, Yuh-Ruey Chen wrote: > But something like: > > x = 10 > def foo > scope.x > end > > would not be as fragile, assuming |scope| is a new builtin method that > does what you'd expect it do. you can write it yourself now cfp:~ > cat a.rb class C include( Module.new do y = 42 def x() y end define_method :method_missing do |_| if((_ = local_variables.index(_.to_s))) eval local_variables[_] else super end end end ) end p C.new.x cfp:~ > ruby a.rb 42 i might add, however, that a function that has access to non-local state it generally called 'a method on an object with instance variables' and ruby has several methods to accomplish this . one very simple method is this cfp:~ > cat a.rb object = Module.new do @x = 42 def y() @x end extend self end p object.y cfp:~ > ruby a.rb 42 and of course there are many more. regards. a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama