This is has been bugging me for quite some time.
>> nil.id (irb):2: warning: Object#id will be deprecated; use Object#object_id => 4
Why would nil.id be 4? (or nil.object_id if you want to be picky about deprecations)
nil is a special Ruby data type that means "nothing". It's equivalent to null or None in other programming languages.
By default methods in Ruby are empty, and an empty method returns, by default, nil.
In Ruby, you can check if an object is nil, just by calling the nil? on the object... even if the object is nil. That's quite logical if you think about it :) Side note : in Ruby, by convention, every method that ends with a question mark is designed to return a boolean (true or false).
nil is an Object, NULL is a memory pointer Sadly, when this happens, Ruby developers are confusing a simple little Ruby object for something that's usually radically different in “blub” language. Often, this other thing is a memory pointer, sometimes called NULL, which traditionally has the value 0.
This is because nil is an object created when the language initializes, and that object's id happens to always be 4.
For more information about why the id happens to be 4, see this blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With