Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: "if !object.nil?" or "if object"

Tags:

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.

like image 689
bigpotato Avatar asked Aug 05 '13 14:08

bigpotato


People also ask

Is nil an object in Ruby?

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.

How do you check if an object is empty in Ruby?

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 .

How do you know if a Ruby is 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).

Is nil empty Ruby?

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.

What is a “nil” in Ruby?

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.

How to avoid checking for nil values in JavaScript?

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

How to return a default value instead of nil in Java?

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.

Is it possible to test for false values in Ruby?

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.


2 Answers

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.

like image 161
Marek Lipka Avatar answered Nov 11 '22 16:11

Marek Lipka


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.

like image 44
Christopher Oezbek Avatar answered Nov 11 '22 15:11

Christopher Oezbek