Ruby 如何确定返回对象的内容?

Ruby 如何确定返回对象的内容?,ruby,ruby-on-rails-3,rubygems,Ruby,Ruby On Rails 3,Rubygems,我认为这很简单,但我已经尝试了好几天没有解决方案。我尝试过的每件事都返回了错误。希望能在这里得到一些帮助 首先,我在这里使用这个文件: 使用有效的tagID(rwk#1242)初始化后 @temp=Covetous::Profile::Career.new'rwk#1242' 我得到一个像这样的返回对象 对象的顶部如下所示: #<Covetous::Profile::Career:0x000001014284c8 @url="http://us.battle.net/api/d3/pr

我认为这很简单,但我已经尝试了好几天没有解决方案。我尝试过的每件事都返回了错误。希望能在这里得到一些帮助

首先,我在这里使用这个文件:

使用有效的tagID(rwk#1242)初始化后

@temp=Covetous::Profile::Career.new'rwk#1242'

我得到一个像这样的返回对象

对象的顶部如下所示:

#<Covetous::Profile::Career:0x000001014284c8 @url="http://us.battle.net/api/d3/profile/rwk-1242/", @response=#<HTTParty::Response:0x102e69ff8 parsed_response={"heroes"=>[{"name"=>"Ziyi", "id"=>10692899, "level"=>60, "hardcore"=>false, "paragonLevel"=>87, "gender"=>1, "dead"=>false, "class"=>"wizard", "last-updated"=>1354337248}, {"name"=>"Aerendil", etc ...
#<Covetous::Profile::Career:0x00000100dfbcf8 @url="http://us.battle.net/api/d3/profile/invalid-9999/", @response=#<HTTParty::Response:0x100d96a88 parsed_response={"code"=>"OOPS", "reason"=>"There was a problem processing the request."}, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"date"=>["Tue, 18 Dec 2012 02:30:00 GMT"], "server"=>["Apache"], "x-frame-options"=>["SAMEORIGIN"], "content-language"=>["en-US"], "vary"=>["Accept-Encoding"], "connection"=>["close"], "transfer-encoding"=>["chunked"], "content-type"=>["application/json;charset=UTF-8"]}>>
#“Ziyi”,“id”=>10692899,“level”=>60,“hardcore”=>false,“paragolevel”=>87,“gender”=>1,“dead”=>false,“class”=>“wizard”,“last updated”=>1354337248},{“name”=>“Aerendil”等。。。
如果我使用无效的tagID初始化(无效#9999)

@temp=Covetous::Profile::Career.new“无效#9999”

我得到一个返回对象,如下所示:

#<Covetous::Profile::Career:0x000001014284c8 @url="http://us.battle.net/api/d3/profile/rwk-1242/", @response=#<HTTParty::Response:0x102e69ff8 parsed_response={"heroes"=>[{"name"=>"Ziyi", "id"=>10692899, "level"=>60, "hardcore"=>false, "paragonLevel"=>87, "gender"=>1, "dead"=>false, "class"=>"wizard", "last-updated"=>1354337248}, {"name"=>"Aerendil", etc ...
#<Covetous::Profile::Career:0x00000100dfbcf8 @url="http://us.battle.net/api/d3/profile/invalid-9999/", @response=#<HTTParty::Response:0x100d96a88 parsed_response={"code"=>"OOPS", "reason"=>"There was a problem processing the request."}, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"date"=>["Tue, 18 Dec 2012 02:30:00 GMT"], "server"=>["Apache"], "x-frame-options"=>["SAMEORIGIN"], "content-language"=>["en-US"], "vary"=>["Accept-Encoding"], "connection"=>["close"], "transfer-encoding"=>["chunked"], "content-type"=>["application/json;charset=UTF-8"]}>>
#“处理请求时出现问题。”}、@response=#、@headers={“date”=>[“Tue,Dec 2012 02:30:00 GMT”]、“server”=>[“Apache”]、“x-frame-options”=>[“SAMEORIGIN”]、“content language”=>[“en-US”]、“vary”=>[“Accept-Encoding”]、“connection”=>[“close”、“transfer-Encoding”=>“chunked”]、“content-type”、“content-type”=>“applicat“application/SAMEORIGIN”]、“content-charset=UTF-8”[]>>
每当给出无效标记时,我需要闪烁一条错误消息

问题是,我怎么知道我得到的是有效的回答还是无效的回答


我试着检查对键“code”或“heros”的响应,但它只在键可用时起作用,否则我会得到一个NoMethodError。有没有办法测试是否有钥匙,返回true/false?

如果要求
code
heros
已知在您正在寻找的情况下会给您一个命名错误,只需将该调用包装在
开始
/
结束
块中,然后用
解救
捕捉错误就行了!谢谢你,马特!Ruby是一门伟大的语言吗?:)