【问题标题】:Correct way to combine `bundle exec` and `ruby -r`?结合`bundle exec`和`ruby -r`的正确方法?
【发布时间】:2020-01-07 06:09:53
【问题描述】:

让我们用一个随机的 gem 来做实验,不管是哪个。

# Gemfile
gem "hashie"

让我们在一个(半)非标准的地方安装我们的包,这样ruby 就找不到它了:

bundle install --path=vendor/bundle
ruby -e 'require "hashie"'
# cannot load such file -- hashie (LoadError)

正如所料,如果我们在上面添加bundle exec,那么ruby -e可以找到我们的gem:

bundle exec ruby -e 'puts require "hashie"'
# true

但是,即使使用bundle exec,ruby -r 也找不到我们的宝石!

bundle exec ruby -r hashie -e 'puts "win"'
# cannot load such file -- hashie (LoadError)

我发现结合bundle exec 和ruby -r 的唯一方法是:

bundle exec ruby -r 'bundler/setup' -r hashie -e 'puts "win"'
# win

documentation for bundle exec 没有提到ruby -r,所以我在这里问一下,这是正确的方式吗?

【问题讨论】:

    标签: ruby bundler


    【解决方案1】:
    -r 'bundler/setup'
    

    是可以接受的。这意味着“需要我的 Gemfile 中的所有宝石”。
    'hashie' 无法加载其依赖项,这就是发生“LoadError”的原因。

    参考:bundler_setup

    【讨论】:

    • 谢谢,该链接很有帮助。不过,我认为Bundler.setup 只配置$LOAD_PATH。它实际上并没有require 任何宝石。
    猜你喜欢
    • 2021-09-22
    • 2014-05-12
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 2011-10-29
    相关资源
    最近更新 更多