为什么我为定义的方法获取NoMethodError?

时间:2018-06-17 20:08:29

标签: ruby

当我尝试运行此脚本时,尽管已经定义了该方法并且需要定义该方法的文件,但我获得NoMethodError方法的getGames

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

1 个答案:

答案 0 :(得分:0)

这是因为res.parsed_responsenil[]正在调用nil

对于id 0,Parsed_response为nil,如果将其更改为1则运行但返回404错误。

您可以使用p res查看HTTParty对象或安装pry,并在返回响应后立即使用require 'pry'; binding.pry