<分区>
在 Rails 中,如果值不存在,我们可以执行以下操作以避免错误:
@myvar = @comment.try(:body)
当我深入挖掘哈希并且不想出错时,有什么等价物?
@myvar = session[:comments][@comment.id]["temp_value"]
# [:comments] may or may not exist here
在上述情况下,session[:comments]try[@comment.id]
不起作用。什么会?