The title of this question may need renaming. Any suggestions?
In my Rails application there was an old Module in the /lib folder called foo_blaster.rb. I then installed my gem named foo_blaster.rb. None of the code in the gem executed until I deleted the original FooBlaster module. Why is this? Shouldn't Ruby combine the two Modules?
The Modules are the same
puts "some characters"
# foo_blaster gem files
FooBlaster::Characters #=> some characters
FooBlaster::Users #=> NameError: uninitialized constant FooBlaster::Users
If you open up a console (with
script/console) and examine the
$LOAD_PATH variable, you should see something resembling this:
>> $LOAD_PATH [ # some entries...  "/your/rails/root/app",  "/your/rails/root/app/controllers",  "/your/rails/root/app/models",  "/your/rails/root/app/helpers",  "/your/rails/root/lib", # more entries...  "/your/ruby/path/lib/ruby/1.8/gems/foo_blaster-0.0.1/lib" # rest of entries ]
require 'foo_blaster' (which may be done implicitly by Rails, Bundler, or perhaps some other means), Ruby searches the locations in
$LOAD_PATH sequentially until it finds the file foo_blaster.rb, loads that file, and stops. In your case, it finds the file in your application's /lib directory. The file from your gem will never be loaded.
If you want to load both files, all you have to do is give them different names and make sure to
require them both. For instance, you might rename the file in your application to foo_blaster_extensions.rb, then add an initializer to load it:
# config/initializers/extend_foo_blaster.rb require 'foo_blaster_extensions'