检查 nil 警告有关意外 nil 的信息 [英] Check for nil warns about unexpected nil

查看:93
本文介绍了检查 nil 警告有关意外 nil 的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 nil 进行了以下检查:

I have the following check for nil:

client = !deal['deal']['party']['party'].nil? ? deal['deal']['party']['party']['company_id'] : ""

但我仍然得到:

当你没想到的时候,你得到了一个 nil 对象!您可能期望有一个 ActiveRecord::Base 实例.评估 nil 时发生错误.[]

You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]

我怎样才能防止这种情况发生?

How can I prevent this?

推荐答案

我不懂 Ruby,但我认为它在 .nil 之前出错了:

I don't know Ruby, but I think it goes wrong before the .nil:

deal['deal']['party']['party']
    ^       ^        ^

箭头表示可能的 nil 索引.例如,如果 ["deal"] 为零或第一个 ["party"] 为零怎么办?

The arrows indicate possible nil indexes. For example, what if ["deal"] is nil or the first ["party"] is nil?

这篇关于检查 nil 警告有关意外 nil 的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆