1
我有如下定义Ruby类:to_json将返回空对象
class MyResponse
attr_writer :status, :message
def initialize(status,message)
@status = status
@message = message
end
end
当我尝试做类型MyResponse的对象上to_json,我适当地得到JSON字符串转换。
问题是,使用传递给MyResponse的同一组参数,我有时会得到空字符串。
例:
response = MyRespons.new("Abcd", 0)
提出response.to_json给出:
"{\"status\": \"Abcd\", \"message\": 0}"
同样有时会:
"\"#<MyResponse:0xb687d884>\""
我的问题是我如何赶上JSON错误?因为在第二种情况下,传递给MyResponse类的参数完全没有区别。
该行为是随机的。
更新:我发现了一个类似的问题。但答案是更新宝石。我无法更新当前的宝石。 Rails: to_json method not working as expected