rails try方法
在rails中,常常要做 是否存在的判断。
例如:
@person && @person.name
or
@person ? @person.name : nil
其实可以使用更简洁的写法
@person.try(:name)
如果@person是 nil 也有 try 这个方法,返回值为 nil。
try 是Object 类 和 Nil 类的方法。
posted on 2011-09-26 14:15 Eric812048774 阅读(269) 评论(0) 编辑 收藏 举报