obj.nil? vs. obj == nil

前端 未结 7 889
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 22:08

Is it better to use obj.nil? or obj == nil and what are the benefits of both?

7条回答
  •  佛祖请我去吃肉
    2020-11-29 22:33

    I find myself not using .nil? at all when you can do:

    unless obj
      // do work
    end
    

    It's actually slower using .nil? but not noticeably. .nil? is just a method to check if that object is equal to nil, other than the visual appeal and very little performance it takes there is no difference.

提交回复
热议问题