gpt4 book ai didi

ruby-on-rails - Httparty::Response 和 try(:parsed_response) 奇怪的行为

转载 作者:数据小太阳 更新时间:2023-10-29 08:12:49 33 4
gpt4 key购买 nike

我尝试在 HttpParty::Response 的一个实例上使用 ActiveSupport 的(2.3 稳定版)try()。我有一个非常奇怪的行为:

> ro.parsed_response
{"error"=>"RecordInvalid", "description"=>"Record validation errors", "details"=>{"email"=> [{"description"=>"Email: foo@bar.com 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 异常。

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

提前致谢!

最佳答案

不知道如何,但似乎 ro 对象被“转移”到已发送给 parsed_response 方法的散列中。
在 Rails 2.3@ruby 1.8 undefined method for object raises an exception .

此外,文档只保留对 nil 对象调用 try 而没有关于没有特定方法的对象:

Unlike that method however, a NoMethodError exception will not be raised and nil will be returned instead, if the receiving object is a nil object or NilClass.

关于ruby-on-rails - Httparty::Response 和 try(:parsed_response) 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15938092/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com