From: Robert Klemme Date: 2007-12-17T06:45:02+09:00 Subject: Re: magic / counter-magic? (detect loading file?) On 16.12.2007 20:52, Giles Bowkett wrote: > Here's something that's puzzling me. > > Somewhere inside Rails is something which loads your .irbrc under some > circumstances. That sounds strange. Why would a rails app need IRB? > It means that if you have a gem which adds a lot of > methods to Object and which is required by people in their .irbrcs, > sooner or later, your methods on Object are going to collide with > Rails' methods on *anything*. I have such a gem, Utility Belt, and > this happened to somebody who installed it. It's going to happen to a > lot of people, in fact, because the method in question was named > "edit," which practically every Rails app uses. > > I took a look around the Rails file-loading process and it looked > pretty involved. I know the problem comes from > ActiveSupport::Dependencies but that's about it. I'd like to find and > fix the bug in Rails but I just don't have time. What I want to do > instead is set up Utility Belt to recognize when it's being called > from Rails and then have it de-borkify the situation. All I really > need to accomplish that is code which can recognize where it's being > called or loaded from. I know such code, and idioms for using it, > exist, but I'm not sure what they are. You can use caller to find out about the call stack: irb(main):003:0> def f(n) caller(n) end => nil irb(main):004:0> f 0 => ["(irb):3:in `f'", "(irb):4:in `irb_binding'", "/usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'", ":0"] irb(main):005:0> f 1 => ["(irb):5:in `irb_binding'", "/usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'", ":0"] irb(main):006:0> f 2 => ["/usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'", ":0"] > Is it just a matter of grepping > against __FILE__? How does my gem determine who it's being loaded by > and send them away if they don't need to be loading it? Alternatively you could set up tracing with set_trace_func and determine the calling chain that leads to this behavior by looking at the trace output. Kind regards robert