In almost every Sinatra example I've seen, despite what it does, it always has the following two lines:
require 'rubygems'
require 'bundler/setup'
In most examples, removing the 'bundler/setup' require seems to have no breaking effect, so I'm confused about when/where I need to include this.
I hate using things without knowing exactly the reason for it being there, so I was hoping someone could explain why I need both lines and what they do?
There is a practical explanation:
Let's say we want to use a gem called pristine_text from the github master branch
Gemfile:
main.rb: (wrong)
require can't find it, because the gem is not in a path ruby can see. If you look at the actual path of the gem, you will see that it is under
/pristine-text-some_commit_id
main.rb: (right)
The error is gone, because now you load bundler with your dependencies' load paths.
The Bundle setup "clears" the load path, so the subsequent attempt to require something that is not in Gemfile will result of the load error.
Understanding Bundler's setup process
Brian Storti wrote the best article I can find on Bundler setup - from which the quote is taken.
Note: I've updated the "show me the code" links as they went to master branch which has changed.
It ensures you're loading Gemfile defined gems. Please have a look at the documentation here https://bundler.io/v1.12/bundler_setup.html