Ruby:在模块内调用gem子类产生未初始化的常量

Ruby:在模块内调用gem子类产生未初始化的常量,ruby,module,Ruby,Module,我有两个项目,一个“通用代码”项目,我把它做成了一个大模块,可以像这样把其他模块拉进来: 以下是“我的公共项目”的文件夹结构: 我的共同项目 普通 rest-client.rb 其他带有模块的ruby文件 common.rb Gemfile 等等 common.rb rest-client.rb Gemfile 然后在另一个名为“我的另一个项目”的项目中: 我的其他项目 服务 service.rb service.rb 当代码进入rest-client.rb中的rescu

我有两个项目,一个“通用代码”项目,我把它做成了一个大模块,可以像这样把其他模块拉进来:

以下是“我的公共项目”的文件夹结构:

  • 我的共同项目
    • 普通
      • rest-client.rb
      • 其他带有模块的ruby文件
    • common.rb
    • Gemfile
    • 等等
common.rb rest-client.rb Gemfile 然后在另一个名为“我的另一个项目”的项目中:

  • 我的其他项目
    • 服务
      • service.rb
service.rb 当代码进入rest-client.rb中的rescue块时,我收到一个错误:

NameError - uninitialized constant Common::RestClient::Exception

我不知道该如何表达我的问题,但在这个例子中,公共模块似乎正在失去rest客户机模块和其他类的优势。有人能解释一下为什么这种集总然后包含许多模块的方法不起作用吗?

因为您有一个名为
Common::RestClient
的类,并且在
Common
模块中使用一个ruby gem来定义一个类
RestClient
,您需要使用前缀
引用gems
RestClient
,否则,它假定您正在谈论
Common::RestClient

module Common
  module RestClient

  def call_rest_service_get(url)
    begin
      response = ::RestClient.get(url, {accept: :json})
    rescue ::RestClient::Exception => err
      return err.response
    else
      return response
    end
  end
end

谢谢这正是我所需要的…也是一件很难找到的事情!“我不知道如何表达我的问题……”的确!很高兴我找到了你的问题。
# frozen_string_literal: true
source 'https://rubygems.org'

gem 'rest-client'
# other gems here...
require_relative './../../../common/common.rb'

class Service

  include Common

  def get_rest_data
    call_rest_service_get('http://some-url.com)
  end
end
NameError - uninitialized constant Common::RestClient::Exception
module Common
  module RestClient

  def call_rest_service_get(url)
    begin
      response = ::RestClient.get(url, {accept: :json})
    rescue ::RestClient::Exception => err
      return err.response
    else
      return response
    end
  end
end