我有一个在铁轨外使用黄瓜的项目。如何使用我的gemfile中指定的版本加载gem?
答案 0 :(得分:23)
Gemfile
(运行bundle init
以创建骨架Gemfile
)bundle install
在您的应用中:
# 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
Bundler.io - Using Bundler in Your Appplication
Bundler.io - Bundler.setup and Bundler.require
答案 1 :(得分:11)
我刚学会了一种让Bundler自动从Gemfile中获取依赖关系的方法。在具有Gemfile的Ruby程序的开头添加此代码:
require 'rubygems'
require 'bundler/setup'
Bundler.require
使用Bundler.require,无需明确要求Gemfile中枚举的gems /库。
此解决方案来自http://technotales.wordpress.com/2010/08/22/bundler-without-rails/
说实话,我不确定是否需要rubygems部分。
答案 2 :(得分:5)
这是最简单,最直接的方法:
require 'bundler/setup'
Bundler.require
将为您创建Gemfile bundler install
fadeOut
以安装宝石。可以(现在)在http://bundler.io找到更多信息。
答案 3 :(得分:2)
Casper有一个非常好的答案(尽管有一些被动的侵略性),但我认为缺少的部分是bundle exec
。在命令行上运行$ rails ...
命令时,Rails使用bundler加载这些依赖项/ gem。例如,Rake默认情况下不是这样,为了使用较旧版本的黄瓜运行rake test
而不是系统上的黄瓜,您必须使用bundle exec rake test
。当你使用Bundler时,使用$ bundle exec ...
进入始终这是一个好习惯 - 它是明确的,你总是确定你正在使用正确的宝石,它确保你不要t忘记为你的Gemfile添加一个依赖(即你推送到另一个服务器或其他开发人员,他们遇到了问题,因为你没有注意到你需要使用的东西,但他们没有。)