# A == nil or a.nil?

**URL:** <https://www.ruby-forum.com/t/a-nil-or-a-nil/99976>\
**Category:** Ruby\
**Created:** [November 23, 2005, 12:05am UTC](https://www.ruby-forum.com/t/a-nil-or-a-nil/99976 "2005-11-23T00:05:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [November 23, 2005, 12:05am UTC](https://www.ruby-forum.com/t/a-nil-or-a-nil/99976/1 "2005-11-23T00:05:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [November 23, 2005, 12:17am UTC](https://www.ruby-forum.com/t/a-nil-or-a-nil/99976/2 "2005-11-23T00:17:21Z")

</div>

On 11/23/05, ako… [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> over the other? also, i am curious to know why nil? has been  
> implemented at all? is ‘== nil’ not enough?
> 
> thanks  
> konstantin

for readability, I wager.

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [November 23, 2005, 12:21am UTC](https://www.ruby-forum.com/t/a-nil-or-a-nil/99976/3 "2005-11-23T00:21:23Z")

</div>

On 11/22/05, ako… [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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)

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [November 23, 2005, 3:26am UTC](https://www.ruby-forum.com/t/a-nil-or-a-nil/99976/4 "2005-11-23T03:26:33Z")

</div>

ako… a écrit :

> implemented at all? is ‘== nil’ not enough?
> 
> thanks  
> konstantin

By the way,  
a == nil  
is also a method call. It is actually equivalent to:  
a.==(nil)

If nil has a == method, why not adding some other practical methods like  
nil? ? Actually, nil? is probably a bit faster than ==(nil).

–  
Lionel T.

Personal web site: [http://users.skynet.be/lthiry/](http://users.skynet.be/lthiry/)

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [November 23, 2005, 1:29am UTC](https://www.ruby-forum.com/t/a-nil-or-a-nil/99976/5 "2005-11-23T01:29:47Z")

</div>

From: “ako…” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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.

Another thing that would be strange in C is:

5.times {print “Hello!”}

🙂 But to me, it’s not worth embracing C’s ideosyncracies when  
programming  
in Ruby. The “everything is an object” consistency of Ruby is an  
elegant  
and enjoyable part of the language.

As an aside, if you’re coming from C, you might be surprised that:

a = 0 # zero  
print “it’s true!” if a # =\> prints “it’s true!”

Zero is not false in Ruby.

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [November 23, 2005, 3:38am UTC](https://www.ruby-forum.com/t/a-nil-or-a-nil/99976/6 "2005-11-23T03:38:37Z")

</div>

On 11/22/05, ako… [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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…

If you’re not feeling the nil methods, remember nil is the only  
Boolean false (besides FalseClass). So, unless you specifically need  
to differentiate between false and nil, you can do this:

if !a  
#stuff  
end

That’s something I see a lot in C, as NULL == false. You might feel  
more at home, and save a method call.

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [November 23, 2005, 6:07am UTC](https://www.ruby-forum.com/t/a-nil-or-a-nil/99976/7 "2005-11-23T06:07:27Z")

</div>

2005/11/23, Rob R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> if !a  
> #stuff  
> end

unless a  
# stuff  
end
