Are they the same when used in an if/else/end
statement? What do you usually do? I'm wondering if there are any subtle differences or edge cases where object
and !object.nil?
would respond differently.
In Ruby, nil is—you've guessed it—an object. It's the single instance of the NilClass class. Since nil in Ruby is just an object like virtually anything else, this means that handling it is not a special case.
You can check if an object is nil (null) by calling present? or blank? . @object. present? this will return false if the project is an empty string or 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).
Well, nil is a special Ruby object used to represent an “empty” or “default” value. It's also a “falsy” value, meaning that it behaves like false when used in a conditional statement.
Nil… What is it, really? Well, nil is a special Ruby object used to represent an “empty” or “default” value. It’s also a “falsy” value, meaning that it behaves like false when used in a conditional statement. There is ONLY one nil object, with an object_id of 4 (or 8 in 64-bit Ruby), this is part of why nil is special.
Depending on what object you are working with there are a few techniques you can use to avoid checking for nil values entirely. For example, you can use the fetch method on Hash & Array objects. Or you can use the “Null Object Pattern” in your own classes if you want to return a default value instead of nil. Other Places Where Nil Is Lurking
Or you can use the “Null Object Pattern” in your own classes if you want to return a default value instead of nil. You should be aware that undefined instance variables will return nil.
Yes, it is the case that nil and false are the only falsy values in Ruby. However, if you are testing for nil by testing for nil || false, this isn't what you mean. It means that the next developer (who happens to be a crazy axe murder who knows your home address) will read the code, and wonder why false should go in there too.
There are differences. For example:
false.nil?
# => false
So:
if !false.nil?
'foo'
end
# => "foo"
if false
'foo'
end
# => nil
As @tokland suggested, in most cases using !obj.nil?
construction is unnecessary.
There is one and only one case in which !object.nil?
and object
evaluate to different results in a boolean context and that is if object
is false
. In all other situations the result is the same.
With this, I think we can answer your real question (which is: Is there any situation where I must use if !object.nil?
instead of just if object
when protecting against object
being nil?):
No, you can always use if object
if you want to check against nil
.
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