Issue #13395 has been updated by ogarci5 (Oliver Garcia).


What about as a condition for if statements? For example:
Case 1
~~~ ruby
if !object.nil?
  # Do something
end
~~~
Case 2
~~~ ruby
if object
  # Do something
end
~~~
Case 3
~~~ ruby
if object.not_nil?
  # Do something
end
~~~

I end up using Case 2 a lot because it reads better than Case 1. However this doesn't work if object can be false in which case Case 3 would be the most readable.

----------------------------------------
Feature #13395: Add a method to check for not nil
https://bugs.ruby-lang.org/issues/13395#change-64063

* Author: JustJosh (Joshua Stowers)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
There does not seem to be a method in Ruby to check if an object is *not* nil.
Such a method could help with readability.

Example:
> ~~~ ruby
> array = [1, 'dog', nil]
> array.count(&:not_nil?)
> ~~~

> vs

> ~~~ ruby
> array = [1, 'dog', nil]
> array.reject(&:nil?).count
> ~~~





-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>