【问题标题】:Bundler: how to use without rails?Bundler:如何在没有导轨的情况下使用?
【发布时间】:2011-08-09 16:29:51
【问题描述】:

我有一个项目在铁轨外使用黄瓜。如何使用我的 gemfile 中指定的版本加载 gem?

【问题讨论】:

    标签: ruby rubygems bundler


    【解决方案1】:

    挖掘the Bundler website

    1. 创建Gemfile(运行bundle init创建骨架Gemfile
    2. bundle install
    3. 在您的应用中:

      # 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

    Are bundle exec and require 'bundler/setup' equivalent?

    【讨论】:

      【解决方案2】:

      我刚刚了解了一种使 Bundler 自动需要 Gemfile 的依赖项的方法。在具有 Gemfile 的 Ruby 程序的开头添加以下代码:

      require 'rubygems'
      require 'bundler/setup'
      Bundler.require
      

      使用 Bundler.require,无需显式要求 Gemfile 中枚举的 gem/库。

      此解决方案来自http://technotales.wordpress.com/2010/08/22/bundler-without-rails/

      说实话,我也不确定是否需要 require ruby​​gems 部分。

      【讨论】:

      • require 'rubygems' 仅在 1.8.7 之前的 Ruby 中需要,如果我没记错的话。
      【解决方案3】:

      这是最简单最直接的方法:

      1. bundler init 将为您创建 Gemfile
      2. 在 Gemfile 中指定 gem。
      3. 将以下内容添加到您的主 Ruby 文件中
      require 'bundler/setup'
      Bundler.require
      
      1. 运行bundler install 安装gem。

      更多信息(现在)可以在http://bundler.io找到。

      【讨论】:

        【解决方案4】:

        Casper 有一个很好的答案(尽管有一些被动的攻击性),但我认为你缺少的部分是 bundle exec。当您在命令行上运行$ rails ... 命令时,Rails 使用捆绑程序来加载这些依赖项/gems。例如,Rake 默认情况下不支持,因此要使用比系统上旧版本的黄瓜运行 rake test,您必须使用 bundle exec rake test。当您使用 Bundler 时,始终使用 $ bundle exec ... 是一个好习惯 - 这是明确的,您始终可以确定您使用的是正确的 gem,并确保您不会忘记向您的 Gemfile 添加依赖项(即,您推送到另一台服务器或另一位开发人员,但他们遇到了问题,因为您没有注意到您需要使用的东西,但他们没有)。

        【讨论】:

        • 是的..我猜在脸颊上的友好舌头并不能很好地通过互联网:-/ 已修复。即使您require "bundler/setup" 也需要bundle exec。 IE。 bundle exec 是否只是为了确保即使没有设置也能正常工作?
        • 我不太熟悉将 Bundler 集成到应用程序中 - 我知道您只需运行 $ bundle exec ruby foo.rb 即可。
        • 好的,谢谢。事实上,我只是查看了bundle exec docs,它似乎确实在执行时将显式的bundler/setup“强制”为红宝石(除其他外)。所以使用bundle exec 是一种额外的保险,一切都将真正使用bundler 运行。它还可以轻松地以这种方式捆绑旧应用程序(或任何应用程序),并且您不必接触任何代码。
        猜你喜欢
        • 1970-01-01
        • 2021-07-03
        • 1970-01-01
        • 2020-10-28
        • 2021-10-30
        • 2012-07-09
        • 2020-10-10
        • 2019-03-19
        • 2013-05-18
        相关资源
        最近更新 更多