【问题标题】:Is require ´bundler/setup' the same as running Bundler.setup?require 'bundler/setup' 和运行 Bundler.setup 一样吗?
【发布时间】:2017-02-28 00:55:44
【问题描述】:

这是:

require 'bundler'
Bundler.setup

完成同样的事情:

require 'bundler/setup'

据我了解,bundler/setup 自动要求所有组,而 require 'bundler' 则不是这种情况。那么考虑到这个事实,这是否意味着上面的 2 个 sn-ps 代码完成了同样的事情?

【问题讨论】:

    标签: ruby-on-rails ruby bundler


    【解决方案1】:

    答案在bundler/setup的源码中找到:

    require 'bundler/shared_helpers'
    
    if Bundler::SharedHelpers.in_bundle?
      require 'bundler'
      if STDOUT.tty?
        begin
          Bundler.setup
        rescue Bundler::BundlerError => e
          ...
        end
      else
        Bundler.setup
      end
    
      ...    
    end
    

    in_bundle? 方法似乎正在检查 Bundler 是否在其内部运行(据我所知,出于测试目的),并验证 Gemfile 是否存在。

    所以是的,对于一般用途,您的两段代码是等效的。

    bundler/setup 版本的优势在于,您可以像这样从命令行运行 Ruby:

    ruby -rbundler/setup ... some_ruby_script.rb
    

    这将自动使您的脚本在 Bundler 下运行,即使脚本本身可能不支持 Bundler,这与 bundle exec 所做的几乎相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-12
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多