From: Eric Mahurin Date: 2005-11-23T08:19:58+09:00 Subject: Re: a == nil or a.nil? On 11/22/05, ako... wrote: > hello, > > this is more of a philosophical question. my C experience makes me feel > more comfortable with expressions such as 'a == nil' rather than > a.nil?. i feel strange when i invoke a method on a nil value. i know, > nil is also an object and not just a zero reference but still it feels > strange... > > would the experts please comment on why one way would be preferable > over the other? also, i am curious to know why nil? has been > implemented at all? is '== nil' not enough? > > thanks > konstantin == is usually meant to query whether the data in the objects are the same (or they are the same object). For most == methods, if the data is not comparable (not the same class), it will return false rather than raise an exception. So... == nil should work fine in most cases. I would use one of these which are more direct: a.nil? nil==a # uses the NilClass#== rather than an arbitrary == a.equal?(a) nil.equal?(a)