Joshua Warchol's Rails Tips

Why do I get "already initialized constant" warnings?

Do you get the following warnings?

/usr/lib/ruby/gems/1.8/gems/xml-simple-1.0.10/lib/xmlsimple.rb:280: warning: already initialized constant DEF_KEY_ATTRIBUTES
/usr/lib/ruby/gems/1.8/gems/xml-simple-1.0.10/lib/xmlsimple.rb:281: warning: already initialized constant DEF_ROOT_NAME
/usr/lib/ruby/gems/1.8/gems/xml-simple-1.0.10/lib/xmlsimple.rb:282: warning: already initialized constant DEF_CONTENT_KEY
/usr/lib/ruby/gems/1.8/gems/xml-simple-1.0.10/lib/xmlsimple.rb:283: warning: already initialized constant DEF_XML_DECLARATION
/usr/lib/ruby/gems/1.8/gems/xml-simple-1.0.10/lib/xmlsimple.rb:284: warning: already initialized constant DEF_ANONYMOUS_TAG
/usr/lib/ruby/gems/1.8/gems/xml-simple-1.0.10/lib/xmlsimple.rb:285: warning: already initialized constant DEF_FORCE_ARRAY
/usr/lib/ruby/gems/1.8/gems/xml-simple-1.0.10/lib/xmlsimple.rb:286: warning: already initialized constant DEF_INDENTATION

Are you using a gem such as 'flickr'? Well, the reason you're getting these warnings is that Ruby on Rails includes its own copy of the XmlSimple library. But XmlSimple is also available as a Ruby Gem, and the flickr gem depends on it. Ruby tries to avoid loading the same library twice, but it does this based on the file name. Problem is, the gem's file is "xmlsimple.rb" but the Ruby on Rails' file is "xml_simple.rb". Bogus.

Right now my suggestion is to forgo the gem for flickr and unpack it into your RAILS_ROOT/lib directory. Then remove the "require 'xmlsimple' line from the file.

(from your RAILS_ROOT)
$ cd lib/
$ gem unpack flickr
 Unpacked gem: 'flickr-1.0.0'
$ cd ../
$ vi config/environment.rb # or use TextMate, whatever..
(Add this at the bottom or wherever you require libraries specific to your site)

# We've unpacked the flickr gem to avoid some
# "already initialized constant" warnings related
# to Rails including its own copy of XmlSimple
require 'lib/flickr-1.0.0/flickr.rb'

(then save and exit your editor)

$ vi lib/flickr-1.0.0/flickr.rb
(now comment out the following line, at or around line #49)
require 'xmlsimple'

(you might want to leave a note in the comments so you remember why you did this)

That's it. I know this isn't the most sexy of solutions, but until something better shows up, it works for me!

Back to Joshua Warchol's Rails Tips


Joshua E. Warchol Fork me on GitHub