Please do not top post. On 02.05.2008 19:26, Ruby Talk wrote: > Well, let's say that that something is a new class I want to define > within that scope. And I'd like the innards of that class to be able to > take advantage of the the natural, idiomatic lookup rules, where the > interpreter traipses up the ancestor chain. > > To be more clear, let's say I have > > X = :bad > module A > X = :good > class B > #...let us say I want to execute something here where X == :good > end > end > > And I'd like to execute something from outside that lexical scope (let's > say from my metaprogramming classes) within that [A, A::B] lexical scope. > > I guess what I'm asking is: is there any way of faking lexical scope > (for the purposes of metaprogramming)? For constant lookup there is: robert@fussel /cygdrive/c/Temp $ ruby lex.rb 2 robert@fussel /cygdrive/c/Temp $ ruby lex.rb 2 2 robert@fussel /cygdrive/c/Temp $ cat lex.rb class Module def lex_lookup(c) name.split('::').inject([TOPLEVEL_BINDING,[]]) do |(b,o),n| b = (b.const_get n rescue eval(n,b)) [b, o << b] end.last.reverse.each do |cl| return cl.const_get(c) if cl.const_defined? c end raise NameError, c end end X=1 class Foo X=2 class Bar puts lex_lookup("X") end puts lex_lookup("X") end robert@fussel /cygdrive/c/Temp $ You can easily extend that to include top level constants as well as other evaluations. Cheers robert