2013-04-10 48 views
3

我试图在HttpParty :: Response的实例上使用ActiveSupport(2.3-stable)try()。我有一个很奇怪的行为:HttpParty ::响应和尝试(:parsed_response)奇怪的行为

> ro.parsed_response 
{"error"=>"RecordInvalid", "description"=>"Record validation errors", "details"=>{"email"=>  [{"description"=>"Email: [email protected] is already taken by another user"}]}} 

> ro.try(:parsed_response) 
NoMethodError Exception: undefined method `parsed_response' for #<Hash:0x11397d5d8> 

在第一个例子中,我使用点符号发送parsed_response消息给ro。它工作正常。在第二个中,我尝试使用ActiveSupport的try()来调用它,并且(令人惊讶地)引发了NoMethodError异常。

不应该尝试()返回nil在这种情况下?为什么它找不到“parsed_response”方法,如果我可以使用点符号来调用它,如第一个例子所示?

在此先感谢!

+0

你是红宝石1.8吗? – ted 2013-04-11 00:04:35

+0

嗨特德,是的,我在Ruby 1.8.7上。 – FullOfCaffeine 2013-04-11 19:32:45

回答

0

不知道如何,但看起来像ro对象被“转移”为已发送parsed_response方法的散列。
In Rails [email protected] 1.8 undefined method for object raises an exception

此外,文件只停留呼吁对象并没有什么关于对象的尝试没有一个具体方法:

但是,与这种方法,一个NoMethodError异常不会被 上调如果接收对象是 无对象或NilClass,则返回nil。

+0

是啊,看起来像。 Ruby的灵活性有时可以反弹给你。感谢您的答复。 – FullOfCaffeine 2013-07-03 19:45:38

+0

@FullOfCaffeine,同意。别客气 (: – ted 2013-07-03 22:11:56