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:
SurveyorControllerMethods
from Surveyor gem. create
method from
SurveyorControllerMethods
which 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:
jquery-rails
for 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:
require 'bunder/setup'
and at the top of application.rb you'll find:
Bundler.require(:default, :development)
This last method call looks back to your Gemfile, then iterates all of the gems that are in the given groups (:default
and :development
in 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 :require
option. 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 performs Bundler.setup
, which takes care of the load path. It prepends (unshifts) to the $LOAD_PATH
for 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With