简体   繁体   中英

Ruby module method not found

I have a Rubygem which contains the following:

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

    ...

When I try to use the gem from my Rails application with BoardGameGem.get_items(id_list, true) , I get the response:

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

Gem.loaded_specs["board-game-gem"].version gives the correct version number, and using module_function did not solve the problem either. BoardGameGem.methods shows that the method, in fact, doesn't exist. I can't figure out what might be causing the method to not show up.

I would have thought it would look more like:

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

EDIT:

If I do this in console:

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

And then I do:

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

I get:

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

Turns out I had a boardgamegem.rb and a board-game-gem.rb, and I was editing the first without realising it. Not my day it seems...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM