[英]Ruby module method not found
I have a Rubygem which contains the following: 我有一个包含以下内容的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
...
When I try to use the gem from my Rails application with BoardGameGem.get_items(id_list, true)
, I get the response: 当我尝试将我的Rails应用程序中的gem与BoardGameGem.get_items(id_list, true)
,我得到了响应:
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. Gem.loaded_specs["board-game-gem"].version
提供了正确的版本号,并且使用module_function
也不能解决问题。 BoardGameGem.methods
shows that the method, in fact, doesn't exist. BoardGameGem.methods
显示实际上该方法不存在。 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. 原来我有一个boardgamegem.rb和一个board-game-gem.rb,我在编辑第一个但没有意识到它。 Not my day it seems... 似乎不是我的日子...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.