Bundler:如何在没有导轨的情况下使用?
我有一个项目使用铁路外的黄瓜。 如何使用我的gemfile中指定的版本加载gem?
挖掘Bundler网站 :
- 创建
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 – 在Appplication中使用Bundler
Bundler.io – Bundler.setup和Bundler.require
bundle exec是否需要’bundler / setup’等效?
我刚学会了一种让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部分。
这是最简单,最直接的方法:
-
bundler init
将为您创建Gemfile - 在Gemfile中指定gems。
- 将以下内容添加到主Ruby文件中
require 'bundler/setup' Bundler.require
- 运行
bundler install
以安装gem。
可以(现在)在http://bundler.io上找到更多信息。
卡斯帕有一个很好的答案(尽管有一些被动的侵略性),但我认为缺少的部分是bundle exec
。 当您在命令行上运行$ rails ...
命令时,Rails使用bundler来加载这些依赖项/ gem。 例如,Rake默认情况下不是这样,为了使用较旧版本的黄瓜进行rake test
而不是系统上的黄瓜,你必须使用bundle exec rake test
。 这是一个很好的习惯, 总是使用$ bundle exec ...
当你使用Bundler时 – 它是明确的,你总是确定你正在使用正确的gem,它确保你不要忘记添加一个依赖到您的Gemfile(即您推送到另一台服务器或其他开发人员并且他们遇到问题,因为您没有注意到您使用的东西的需要,但他们没有)。