Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Ruby use nil to name the null object?

Tags:

ruby

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?

like image 972
B Seven Avatar asked Mar 01 '14 00:03

B Seven


People also ask

Is nil the same as null Ruby?

Let's start out with “Nil” since it's the most common and easy-to-understand way of representing nothingness in Ruby. In terms of what it means, Nil is exactly the same thing as null in other languages.

What does nil mean in Ruby?

In Ruby, nil is a special value that denotes the absence of any value. Nil is an object of NilClass. nil is Ruby's way of referring to nothing or void.

Is nil the same as null?

NULL and nil are equal to each other, but nil is an object value while NULL is a generic pointer value ((void*)0, to be specific). [NSNull null] is an object that's meant to stand in for nil in situations where nil isn't allowed. For example, you can't have a nil value in an NSArray.

What does nil mean in rails?

nil is a special Ruby data type that means "nothing". It's equivalent to null or None in other programming languages.


1 Answers

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.

like image 52
Chuck Avatar answered Oct 18 '22 09:10

Chuck