【问题标题】:Use ruby gem from git repo via Bundler通过 Bundler 使用来自 git repo 的 ruby​​ gem
【发布时间】:2013-04-20 14:34:26
【问题描述】:

我使用 bundler 创建了一个自定义 gem,并在 git repo 中检查了它。

custom_gem.rb

require "custom_gem/version"
require "custom_gem/custom_class"

module CustomGem
  # Your code goes here...
end

custom_class.rb

require 'typhoeus'

module CustomGem

  class CustomClass

    def self.custom_method
       #do stuff with typhoeus
    end

  end

end

将其作为依赖项添加到另一个项目并通过捆绑程序安装。

gem 'custom_gem', :git => 'git@bitbucket.org:dir/repo.git'

之后我尝试通过调用来使用它

CustomGem::CustomClass.custom_method

我收到以下错误:

未初始化的常量 CustomGem::CustomClass

有什么建议吗?

可能是一件小事,但只是从 ruby​​ 开始,所以任何建议都会很棒。

【问题讨论】:

    标签: ruby git gem bundler


    【解决方案1】:

    要检查两件事:

    1. 运行 Bundle install 时会发生什么(gem 是否列为已安装?)
    2. 您是否需要正确地使用 gem(需要 'custom_gem')? Rails 在那里有一点魔力,但我不确定你是否在使用 rails 应用程序。

    【讨论】:

    • 捆绑安装列出了 gem。我没有使用 rails,但我已经包含了 require 语句
    • 你能在使用它的应用程序中尝试一个简单的定义吗?(CustomGem)(它可以让我们知道它是未知的整个模块,还是只知道里面的类)。
    • 看跌期权定义了吗? CustomGem 输出常量
    • 到了那里,所以你的 gem 被安装了,你的模块(CustomGem)从你的应用程序中是已知的(根据定义的?输出),所以看起来问题出在类上。仔细检查名称(错字确实发生)并根据约书亚的回答路径。如果您觉得一切正常,请发布它们(本质上是 gem 文件结构)。
    【解决方案2】:

    文件custom_gem.rb应该在lib/custom_gem.rb,文件custom_class.rb应该在lib/custom_gem/custom_class.rb

    lib/custom_gem/custom_class.rb
    \_/ \________________________/
     |             |
     |              \_ comes from your code: `require "custom_gem/custom_class"`
     |
     |
      \_ comes from custom_gem.gempsec (the line `s.require_paths = ["lib"]`)
    

    有关加载路径、文件层次结构和命名的更多信息,请查看此gem guide

    【讨论】:

    • 这些是我目前使用的路径,所以理论上应该没问题
    • 好的,然后列出一个大清单:您是否使用bundle exec 运行代码? require 语句返回什么? $ bundle exec gem which custom_gem 返回什么?如果您使用require "custom_gem/custom_class" 直接要求该文件会发生什么?是否可能需要另一个与 custom_gem 同名的文件?
    • 我运行了 bundle exec gem 并获得了 gem 的存储路径。看起来它没有从 git repo 获取我的最新文件,因为我的 custom_class.rb 文件中没有指定模块
    • 我认为每次在 repo 中进行更改时我都需要重新安装 gem,对此不确定
    • 如果你同时在gem和app上开发,最简单的方法是使用路径选项:gembundler.com/v1.3/gemfile.html,这样你就不需要每次都重新安装了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多