如果对象为nil,则不应评估代码中的“无nil到字符串的隐式转换”

时间:2019-09-30 11:03:22

标签: ruby-on-rails ruby

这是代码:

if article.link && article.link.starts_with?(`https://www.youtube.com`) then article.link end

该错误涉及此部分:

article.link.starts_with?(`https://www.youtube.com`)

但是,如果article.link为nil,则甚至不应该评估该部分。

1 个答案:

答案 0 :(得分:1)

如果问题出在org.omg.CORBA.MARSHAL: Buffer overread by 4 bytes at org.openorb.io.AbstractInputStream.next(Unknown Source) at org.openorb.iiop.CDRInputStream.read_long(Unknown Source) at org.openorb.iiop.CDRInputStream.read_ulong(Unknown Source) at org.openorb.iiop.CDRInputStream.read_wstring(Unknown Source) at imap.TraceService.TraceTask_THelper.read(Unknown Source) at imap.TraceService._TraceService_ITFStub.getTaskById(Unknown Source) ,则错误将是article.link

实际上,问题出在NoMethodError: undefined method `starts_with?' for nil:NilClass表达式上。计算结果为https://www.youtube.com。为什么使用这些报价?将它们更改为nil""