Skip to content

Instantly share code, notes, and snippets.

@akiatoji
Created June 25, 2012 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akiatoji/2990910 to your computer and use it in GitHub Desktop.
Save akiatoji/2990910 to your computer and use it in GitHub Desktop.
awesome_print uninitialized constant BSON error fix

Started getting error below with edge mongoid.

aki@rydeen~/Dropbox/RubyProjects/zitta-orders > rails c
Loading development environment (Rails 3.2.6)
ruby-1.9.2-p290 :001 > ap 'wheee'
NameError: uninitialized constant BSON
	from /Users/aki/.rvm/gems/ruby-1.9.2-p290/gems/awesome_print-1.0.2/lib/awesome_print/ext/mongoid.rb:23:in `cast_with_mongoid'
	from /Users/aki/.rvm/gems/ruby-1.9.2-p290/gems/awesome_print-1.0.2/lib/awesome_print/formatter.rb:24:in `format'
	from /Users/aki/.rvm/gems/ruby-1.9.2-p290/gems/awesome_print-1.0.2/lib/awesome_print/inspector.rb:104:in `unnested'
	from /Users/aki/.rvm/gems/ruby-1.9.2-p290/gems/awesome_print-1.0.2/lib/awesome_print/inspector.rb:71:in `awesome'
	from /Users/aki/.rvm/gems/ruby-1.9.2-p290/gems/awesome_print-1.0.2/lib/awesome_print/core_ext/kernel.rb:10:in `ai'
	from /Users/aki/.rvm/gems/ruby-1.9.2-p290/gems/awesome_print-1.0.2/lib/awesome_print/core_ext/kernel.rb:15:in `ap'
	from (irb):1
	from /Users/aki/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
	from /Users/aki/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
	from /Users/aki/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
	from script/rails:6:in `require'
	from script/rails:6:in `<main>'

This is because awesome_print uses ::BSON::ObjectId constant which got renamed to Moped::BSON::ObjectId (by commit http://goo.gl/RSZos)

To get ap working again, add the following to bottom of your application.rb file:

BSON=Moped::BSON
@lieldulev
Copy link

Since you only need it in irb/pry, I think putting it in the ~/.irbrc or ~/.pryrc is better since it solves the issue for all projects and keeps your application code clean.
you can put it in a if or a begin rescue in case you work on non mongoid projects ;)

@akiatoji
Copy link
Author

akiatoji commented Aug 1, 2012

You are quite right that we should try to avoid putting this in application.rb. The problem actually is inside awesome_print. It is referring to ::BSON::ObjectId which was a constant in Mongoid 2 that is now deprecated in Mongoid 3. Perhaps I should wait a little and if things indeed stay this way in Mongoid 3, send a pull request to awesome_print.

@rocknrollMarc
Copy link

I know why Its because of awesome_print gem if you leave it away then you dont need BSON::MOPED just mongoid, github: mongoid/mongoid

@fusillicode
Copy link

Sorry for revamping an old discussion but I can't really solve the problem.

I'm not using Rails but only Mongoid and plain Ruby and if I put BSON = Moped::BSON where I want to perform the pretty print what I get is uninitialized constant Moped::BSON (NameError).

All the stuff I'm doing is right inside a gem and so all the dependencies are stored inside the name_of_my_gem.gemspec file.

Thanks in advance for any help will come and sorry for the bother.

Cheers.

@fusillicode
Copy link

Ok I solved the problem by specifing Moped::BSON = BSON before actually using the awesome printing features.

It seems that, for the version of the libraries I'm using (i.e. mongoid 4.0.0, moped 2.0.0 and awesome_print 1.2.0), the BSON constant is correctly defined in contrast to the Moped::BSON.

I hope this can be helpful for the ones will face the same problem.

Cheers.

[EDIT]

Just for the record I've encapsulated the over mentioned "patch" inside a module:

module PatchedAwesomePrint
  require 'awesome_print'
  ::Moped::BSON = ::BSON
end

In this way all you should do is to include PatchedAwesomePrint instead of require 'awesome_print'.

As before I hope this can be helpful but moreover I hope that my refined solution will not break things under specific circumstances. If this is the case forgive me and don't hesitate to point it out by writing it down here.

Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment