From: "Florian Groß" Date: 2005-08-05T11:55:02+09:00 Subject: Re: Need help with with Binding.of_caller Sascha Ebach wrote: > (hoping that Florian is reading) If in doubt CC, but I try to keep up with ruby-talk. (Not true for the Rails list, though -- I tried creating a gmane RSS filter for my identity so I wouldn't miss anything, but so far it seems not to have worked.) > So I thought I might try to use the Binding.of_caller technique. I came > as far as this: > > class String > def interpolate(caller) # the param is not needed anymore > Binding.of_caller do |binding| > eval('"' << self.gsub('"', '\"') << '"', binding) > end > end > ... > end > > which will do exactly the opposite. Now I am able to eval local > variables, but not the instance vars. Trust me: I'm as shocked as you are. I think I actually stumbled upon this behavior before but quickly displaced the knowledge from my memory. I think this is related to Ruby not setting the .self field of the BLOCK struct for trace_func bindings. From what I see binding() has some extra logic which might do exactly this. Don't take my guess at this too literally -- the code for this is fairly complex and I will not claim to understand how all parts of it work together, but I think this might be a bug or a known limitation. You're only chance of really finding out is if you get lucky and Nobu or ts or someone else with deeper Ruby knowledge accidently reads all this. Which would actually be cool because then I would know if this is by design or not. Let's wait a bit and see if anyone bites. I will want to eventually document this so an explanation would be very cool. > What do I need to do to capture the whole environment of the originating > _i() call. Is it at all possible? It's a good question and I would like to say that coding interpolation is evil, but it might make sense here. Perhaps you can make interpolate a method on Kernel -- then you would be able to do interpolate(@foo) and the self of the interpolate method and the one of the caller would always be the same thus avoiding the whole situation. Does this sound reasonable?