Bundler: how to use without rails?
I have a project using cucumber outside o开发者_C百科f rails. How can I load the gems with the versions specified in my gemfile?
Digging through the Bundler website:
- Create
Gemfile
(runbundle init
to create skeletonGemfile
) bundle install
In your app:
# Only needed for ruby 1.8.x require 'rubygems' # The part that activates bundler in your app require 'bundler/setup' # require your gems as usual require 'some_gem' # ...or require all the gems in one statement Bundler.require
Could be worth checking out:
Bundler.io - Using Bundler in Your Appplication
Bundler.io - Bundler.setup and Bundler.require
Are bundle exec and require 'bundler/setup' equivalent?
I just learned about a way to make Bundler automatically require dependencies from a Gemfile. Add this code at the beginning of a Ruby program that has a Gemfile:
require 'rubygems'
require 'bundler/setup'
Bundler.require
With Bundler.require there's no need to explicitly require the gems/libraries enumerated in the Gemfile.
This solution is from http://technotales.wordpress.com/2010/08/22/bundler-without-rails/
To be honest I'm not sure if the require rubygems part is needed either.
Here's the simplest and most straightforward approach:
bundler init
will create the Gemfile for you- Specify gems in the Gemfile.
- Add the following to your main Ruby file
require 'bundler/setup'
Bundler.require
- Run
bundler install
to install the gems.
More information can (now) be found at http://bundler.io.
Casper has a pretty good answer (despite some passive aggressiveness) but I think the missing piece for you is bundle exec
. When you run the $ rails ...
commands on the command line, Rails uses bundler to load those dependencies/gems. Rake, for example, doesn't by default so in order to run rake test
using an older version of cucumber than what is on your system, you have to use bundle exec rake test
. It's a good habit to get into always using $ bundle exec ...
when you're using Bundler — it's explicit, you're always sure you're using the right gems, and it ensures you don't forget to add a dependency to your Gemfile (i.e. you push to another server or another developer and they are having issues because you didn't note the need for something you use but they don't).
精彩评论