From: Gavin Sinclair Date: 2004-08-18T08:13:58+09:00 Subject: Re: Getting the binding of the caller On Wednesday, August 18, 2004, 8:52:18 AM, Michael wrote: > Hi, > Is there a way to get the binding of the caller? > class A > def m(b=binding) > eval("self", b) > end > end > A.new.m # => # > A.new.m(binding) # => main > What I want is exactly the second, but without specifying "binding" > explicitly. > Possible or not? It's been discussed on -talk this year, so a search in the archive for 'binding' should help. Someone posted a continuations-based solution that gives you basically what you want, but at considerable runtime expense, IIRC. There's no plain-Ruby solution; i.e. the language doesn't go out of its way to help you here. There's an RCR for it; can't remember which one. However, blocks can be useful here, and may just be good enough for you. def trace(&block) expr = block.call value = eval expr, block puts "#{expr} = #{value}" end def foo x = 5 y = 12 trace { "Math.sqrt( x**2 + y**2 )" } end I gotta say that's pretty handy. Cheers, Gavin -- Gavin's vim tip for the day: ':help text-objects'