From: Robert Klemme Date: 2011-06-14T18:53:40+09:00 Subject: Re: A way to find out when a constant gets defined? On Tue, Jun 14, 2011 at 11:32 AM, Josh Cheek wrote: > Hi, I'd like to be able to find out when a constant gets defined. I think I > have a gem that vendored Psych, so it doesn't get listed in my dependencies, > but causes the most unusual and difficult to track bugs (because other > things, like rake tasks that don't load the entire environment, don't get > Psych loaded, and thus behave differently when doing things like serializing > data). > > > Here is my reasoning: > > # I do not even have psych installed (it isn't listed) > $ gem list psych > > *** LOCAL GEMS *** > > > # Psych is not a dependency (no output) > $ cat Gemfile.lock | grep -i psych > > > # When I load irb with my app, Psych is somehow defined > $ bundle exec irb > ruby-1.9.2-p180 :001 > Psych >  => Psych > ruby-1.9.2-p180 :002 > exit > > > # When I load irb without my app, Psych is not defined > $ irb > ruby-1.9.2-p180 :001 > Psych > NameError: uninitialized constant Object::Psych > from (irb):1 > from /Users/josh/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `
' > ruby-1.9.2-p180 :002 > exit > > > # For each file in my dir, exempting files in ./git, list the lines matching > /psych/i > # prints nothing, so Psych is not coming from my code. > $ find . -name \* | ruby -ne 'puts $_ unless /^\.\/\.git/' | xargs grep -i > psych > > > > I don't really know what to do, I tried `$ bundle package` and then > untarring and gunzipping all the gems, and then grepping them for it, but > didn't find anything. I can't figure out where this is coming from, so I'd > like to be able to monitor the constant Psych to find out when it gets set. These options come to mind: Define constant Psych yourself before any requires and watch out for the warning "already initialized constant" which also mentions file and line number. Use set_trace_func lambda {|*a| p a} before the first require and try to see whether you can find out from there psych code is loaded. $ find /c/Archives/ruby-lang.org/ruby-1.9.2-p180 -name '*.[ch]' | xargs fgrep -nw Psych /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:36:/* call-seq: Psych::Emitter.new(io) /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:38: * Create a new Psych::Emitter that writes to +io+. /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:54: * See Psych::Handler#start_stream /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:74: * See Psych::Handler#end_stream /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:94: * See Psych::Handler#start_document /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:159: * See Psych::Handler#end_document /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:179: * See Psych::Handler#scalar /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:217: * See Psych::Handler#start_sequence /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:247: * See Psych::Handler#end_sequence /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:267: * See Psych::Handler#start_mapping /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:297: * See Psych::Handler#end_mapping /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:316: * See Psych::Handler#alias /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/emitter.c:388: VALUE psych = rb_define_module("Psych"); /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/parser.c:49: * See Psych::Parser and Psych::Parser#handler /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/parser.c:292: mPsych = rb_define_module("Psych"); /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/psych.c:3:/* call-seq: Psych.libyaml_version /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/psych.c:25: mPsych = rb_define_module("Psych"); /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/to_ruby.c:33: VALUE psych = rb_define_module("Psych"); /c/Archives/ruby-lang.org/ruby-1.9.2-p180/ext/psych/yaml_tree.c:17: VALUE psych = rb_define_module("Psych"); :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/