Finding the guilty

Blog ยป Finding the guilty

Posted on 14 Dec 2010 15:21

This is a story about debugging deeply into Rails using method hooks and dumping the stack. Suddenly I've found out that rake tasks were not using correct pluralizations. Out of curiosity added those lines to config/initialize/inflections.rb

puts "'DEBUG plural: via String' #{'event_series'.pluralize}"
puts "DEBUG plural via Inflector #{ActiveSupport::Inflector.pluralize('event_series')}"

The outputs were not matching. So class String was not using the inflector anymore.

How did I find how to compare those? I issued an

grep -R 'def pluralize'  /usr/lib/ruby/gems/

Anyway, so who was modifying the String class?

Then I found the definition of String and added:

class String
   def self.method_added( mtd )
     puts "Debug mtd: #{mtd}" if mtd == :pluralize
     if mtd == :pluralize
       puts "Debug: #{caller}"

Whenever somebody redefines the :plural method, I would get a call stack dump. I did. The guilty was Extliib library.

I added this to my config/initialize/inflections.rb to temporarily solve the problem:

class String
  def pluralize

In summary. Using :method_added, you can hunt who is spoiling a Ruby class.

But guess what. All this scenario is not very realistic. Because Extlib and ActiveSupport conflict would not cover only one method… Just for the sake of example. What could be done…

If you like this page, please spread the word: diggdel.icio.usFacebook

You can contact me if you have questions or corrections.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License