2

Possible Duplicate:
obj.nil? vs. obj == nil

Working through ruby koans now and found this comment embedded in one of the .rb files:

    # THINK ABOUT IT:
#
# Is it better to use
#    obj.nil?
# or
#    obj == nil
# Why?

I know from convention the answer is obj.nil? but I could not tell you why. Anyone have any ideas?

Community
  • 1
  • 1
Evan V
  • 1,837
  • 1
  • 12
  • 10

1 Answers1

3

The reason is because it is more common to override #== method than #nil? method. So obj.nil? will more reliably do what it is expected to do.

Boris Stitnicky
  • 12,444
  • 5
  • 57
  • 74