Вопрос

I've tried to use ActiveSupport's (2.3-stable) try() on an instance of HttpParty::Response. I've got a pretty strange behavior:

> 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>

In the first example, I send the parsed_response message to ro by using the dot notation. It works fine. In the second one, I try to call it using ActiveSupport's try(), and it (surprisingly) raises the NoMethodError exception.

Shouldn't try() have returned nil in this case? And why doesn't it find the "parsed_response" method, if I can call it using the dot notation, as seen in the first example?

Thanks in advance!

Это было полезно?

Решение

Have no idea how, but seems like ro object was "transfered" into a hash that has been sent a parsed_response method.
In Rails 2.3@ruby 1.8 undefined method for object raises an exception.

Besides, docs stay only for calling try on nil object and nothing about the object that doesn't have a specific method:

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top