找不到Ruby模块方法

时间:2016-05-17 02:23:24

标签: ruby-on-rails ruby rubygems

我有一个包含以下内容的Rubygem:

module BoardGameGem
    API_ROOT = "https://www.boardgamegeek.com/xmlapi2"
    MAX_ATTEMPTS = 10

    def BoardGameGem.get_item(id, statistics = false, options = {})
        options[:id] = id
        options[:stats] = statistics ? 1 : 0
        item = BGGItem.new(BoardGameGem.request_xml("thing", options))
        return item.id == 0 ? nil : item
    end

    def BoardGameGem.get_items(ids, statistics = false, options = {})
        options[:id] = ids.join(",")
        options[:stats] = statistics ? 1 : 0
        item_xml = BoardGameGem.request_xml("thing", options)
        item_list = []
        item_xml.css("item").wrap("<item_data></item_data>")
        item_xml.css("item_data").each do |item_data|
            item_list.push(BGGItem.new(item_data))
        end
        item_list
    end

    ...

当我尝试使用BoardGameGem.get_items(id_list, true)的Rails应用程序中的gem时,我收到了回复:

undefined method `get_items' for BoardGameGem:Module Did you mean? get_item

Gem.loaded_specs["board-game-gem"].version提供了正确的版本号,使用module_function也无法解决问题。 BoardGameGem.methods表明该方法实际上不存在。我无法弄清楚可能导致该方法无法显示的原因。

2 个答案:

答案 0 :(得分:2)

我原以为它看起来更像是:

module BoardGameGem
  API_ROOT = "https://www.boardgamegeek.com/xmlapi2"
  MAX_ATTEMPTS = 10

  def self.get_item(id, statistics = false, options = {})
    ...
  end

  def self.get_items(ids, statistics = false, options = {})
    ...
  end
end

编辑:

如果我在控制台中这样做:

module BoardGameGem

  def self.get_item(id, statistics = false, options={})
  end

  def self.get_items(ids, statistics = false, options={})
    puts "ids: #{ids}"
    puts "statistics: #{statistics}"
  end

end

然后我这样做:

BoardGameGem.methods.include?(:get_item)
BoardGameGem.methods.include?(:get_items)
BoardGameGem.get_items([1,2], true)

我明白了:

irb(main):088:0> BoardGameGem.methods.include?(:get_item)
=> true
irb(main):089:0> BoardGameGem.methods.include?(:get_items)
=> true
irb(main):090:0> BoardGameGem.get_items([1,2], true)
ids: [1, 2]
statistics: true
=> nil

答案 1 :(得分:0)

原来我有一个boardgamegem.rb和一个board-game-gem.rb,我正在编辑第一个没有意识到它。不是我的一天......