obj.nil? vs. obj == nil
问题描述:
是否最好使用 obj.nil?
或 obj == nil
?
答
最好使用obj.nil吗? or obj == nil
这完全一样。从外部(pfff) *
It is exactly the same. It has the exact same observable effects from the outside ( pfff ) *
可观察到的效果, / em>
and what are the benefits of both.
如果您喜欢微优化全部,对象将返回 false除了对象
nil
本身的 .nil?
消息 ==
消息将与另一个对象执行微小的微比较
,以确定它是否是同一个对象。
If you like micro optimizations all the objects will return false
to the .nil?
message except for the object nil
itself, while the object using the ==
message will perform a tiny micro comparison
with the other object to determine if it is the same object.
* 查看评论。