This question is about bundler,rubygems & how does it figure out which method I am calling. Its quite long, so please bear with me.
As per my understanding, Bundler is a dependency management tool for managing gems for ruby. It install all the gems & their dependencies listed in the Gemfile.
The question I want to ask can be best illustrated by an example. so here–> In my rails app I am doing this in my controller:-
module SurveyorControllerCustomMethods
def create
super
end
end
class SurveyorController < ApplicationController
include Surveyor::SurveyorControllerMethods
end
Here, I am doing two things:
- Include
SurveyorControllerMethodsfrom Surveyor gem. - As I have used super here, it would call
createmethod from
SurveyorControllerMethodswhich works just fine but I do not
understand it.
and the gem is installed at
$ bundle show surveyor
/home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/surveyor-0.22.0
Which surprisingly is NOT present in ruby’s $LOAD_PATH. so question is:
- how does it even work?
- Install a gem using bundler, what happens behind the scenes during install?
- there are some gems ex
jquery-railsfor which we even don’t have to include/call them in our code & yet, jQuery JavaScript file is automatically included. I know its not magic, so how does this work?
They key is actually in how Rails is set up out of the box. Which is also one of the reasons Rails is so slow to start on larger projects.[1]
In boot.rb you’ll find:
and at the top of application.rb you’ll find:
This last method call looks back to your Gemfile, then iterates all of the gems that are in the given groups (
:defaultand:developmentin the above). It then takes a stab at determining what the default file is that should be loaded, which is typically the gem name, but can also be specified by a:requireoption. Bundler has some built-in naming convention exceptions too, such as the handling of “-” instead of “_”.Bundler then just loads the primary file for the gem, which in turn (typically) loads the files inside the gem.
The
'bundler/setup'file performsBundler.setup, which takes care of the load path. It prepends (unshifts) to the$LOAD_PATHfor each gem. The actual information about what those loads paths should be comes from rubygems.Take a look at the source in:
https://github.com/carlhuda/bundler/blob/master/lib/bundler/setup.rb
https://github.com/carlhuda/bundler/blob/master/lib/bundler/runtime.rb
[1]: When you start a new Rails project, you’ll probably find it doesn’t get so slow over time if you delete the Bundler.require line and just require gems as they are needed.