ruby

Why does Ruby use nil to name the null object?


Googled but did not find an answer.

Is there something special about the name nil?

Is the concept of nil different from the null in other languages?


Solution

  • Well, "nil" is the traditional name for the reified concept of "nothing" in Lisp and Smalltalk†. The word "null" is used as an adjective meaning "empty", as in "the null list," which is nil.

    Meanwhile, "null" is traditionally a pointer value in C that signifies the pointer doesn't point to anything valid. It refers to the fact that the pointer is null (in the same sense that Lisp uses the word), but it came to be thought of as a value on its own.

    Matz was a fan of both Smalltalk and Lisp, so he went with their terminology. There isn't really an important difference in meaning between the two terms — one is just C-ish.

    Actually, "nil" existed in a lot more languages than just those. Even Algol-68, the great granddaddy of C, called it "nil". I'm not sure if C invented "null" as the name for the null reference or just popularized it, but I'm pretty sure that all the modern languages using that term got it from C.