bundle.setup和bundle.require有什么区别 [英] what's the difference between bundle.setup and bundle.require

查看:126
本文介绍了bundle.setup和bundle.require有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gemfile手册页,我了解到有两种方法可以导入您在 Gemfile 中指定的宝石。 bundle.setup 会将gem添加到Ruby的加载路径,而 bundle.require 将需要所有gem。

这两种方法有什么区别?在哪种情况下我应该使用其中的一种?

解决方案

Bundler.setup 修改LOAD_PATH,所以你可以做一些事情,比如 require'some_gem',它们会工作。它可以让你'手工'需要宝石。在Bundler之前,使用Rubygems,您可以在 require'rubygems'中获得相同的效果。



<$另一方面,c $ c> Bundler.require(:default)实际上需要Gemfile中的所有gem(假设你没有使用组;否则它需要那些在指定组中参数)。它是一堆 require'some_gem'语句的缩写。



请参阅http://gembundler.com/rationale.html 。请注意,他们说在做 Bundler.require 之前你必须做 require'bundler / setup',但在实践中通常没有必要。我几乎从不使用 Bundler.setup (或 require'bundler / setup ),因为我需要所有宝石通过 Bundler.require )。


From the gemfile man page, I learned there are two ways to import the gems you specified in the Gemfile. The bundle.setup will "setup adds gems to Ruby's load path" while bundle.require will require all the gems.

What's the difference between these two methods? In which condition should I use one of them?

解决方案

Bundler.setup modifies the LOAD_PATH, so you can do things like require 'some_gem' and they will work. It allows you to require gems 'by hand'. Before Bundler, using Rubygems, you would achieve much of the same effect doing require 'rubygems'.

Bundler.require(:default) on the other hand actually requires all the gems in the Gemfile (assuming you're not using groups; otherwise it requires those in the specified groups if you provide arguments). It is a shorthand for a bunch of require 'some_gem' statements.

See http://gembundler.com/rationale.html. Note that they say you have to do require 'bundler/setup' before doing Bundler.require, but in practice this usually is not necessary. I almost never use Bundler.setup (or require 'bundler/setup), because I require all gems via Bundler.require).

这篇关于bundle.setup和bundle.require有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆