Ruby 为什么我会得到一个已定义方法的NoMethodError?

Ruby 为什么我会得到一个已定义方法的NoMethodError?,ruby,Ruby,当我尝试运行这个脚本时,我得到了getGames方法的NoMethodError,尽管已经定义了该方法并需要定义它的文件 index.rb require './steam.rb' getGames() steam.rb def getGames() currentID = 0 while currentID < 100 # !!TODO!! Change to Production Value # Get JSON of Game url = "http:/

当我尝试运行这个脚本时,我得到了
getGames
方法的
NoMethodError
,尽管已经定义了该方法并需要定义它的文件

index.rb

require './steam.rb'

getGames()
steam.rb

def getGames()
  currentID = 0
  while currentID < 100 # !!TODO!! Change to Production Value

    # Get JSON of Game
    url = "http://store.steampowered.com/api/appdetails?appids=#{ currentID }"
    res = HTTParty.get(url)
    pRes = res.parsed_response["#{ currentID }"]

    # Check For Eligibility

    # Get Eligibility Info
    begin
      type = pRes["data"]["type"] == 'game' ? true : false
      success = pRes["success"]
      paid = pRes["data"].has_key? "price_overview"

      if type && success && paid
        name = pRes["data"]["name"]
        appid = currentID
        img = pRes["data"]["header_image"]
        description = pRes["data"]["detailed_description"]
        dev = pRes["data"]["developers"][0]
        publisher = pRes["data"]["publishers"][0]

        Mongolize(name, appid, img, description, dev, publisher)
      elsif !type
        puts "Not a Game"
      elsif !success
        puts "API call Failed"
      elsif !paid
        puts "Free Game"
      end
    rescue NoMethodError
      puts "ERROR: 404: JSON Object does not Exist"
    end

    # Iterator
    currentID += 10
  end
end
def getGames()
当前ID=0
当currentID<100时!!待办事项!!产值变动
#获得游戏的JSON
url=”http://store.steampowered.com/api/appdetails?appids=#{currentID}”
res=HTTParty.get(url)
pRes=res.parsed_响应[“#{currentID}”]
#检查资格
#获取资格信息
开始
类型=压力[“数据”][“类型”]=“游戏”?对:错
success=pRes[“success”]
PAYED=pRes[“数据”]。有密钥吗?“价格概述”
如果类型为&&success&&paid
name=pRes[“数据”][“名称”]
appid=当前ID
img=pRes[“数据”][“标题图片”]
description=pRes[“数据”][“详细描述”]
dev=pRes[“数据”][“开发人员”][0]
publisher=pRes[“数据”][“发布者”][0]
蒙古化(名称、appid、img、说明、开发人员、发布者)
埃尔西夫!类型
“不是游戏”
埃尔西夫!成功
放置“API调用失败”
埃尔西夫!支付
“免费游戏”
结束
救援指名员
放置“错误:404:JSON对象不存在”
结束
#迭代器
当前ID+=10
结束
结束

这是因为
res.parsed\u response
nil
并且
[]
nil
上被调用

对于id 0,解析的_响应为
nil
,如果将其更改为1,它将运行,但返回404个错误


您可以使用
p res
查看
HTTParty
对象或安装pry并使用
要求的“pry”;binding.pry
仅在返回响应之后。

在一个文件中定义的方法不适用于该文件。否则,就不可能将一个应用程序拆分为多个文件。如果这是真的,那么标准库和RubyGems生态系统的整个概念就不可能存在,因为标准库和gems的全部要点是使用在不同文件中定义的方法。错误消息会准确地告诉您在哪一行的哪个对象上调用了哪个方法。有了这些信息,您应该能够自己找到错误。如果没有这些信息,我们就无法帮助您。但是我如何纠正这一点(即解析响应,而不是让它为零)?您不能,它为零。除非res.parsed_response
,否则您可以执行类似于添加
的操作,这将跳过返回无解析响应对象的ID。