From: shevegen@... Date: 2017-04-08T20:02:51+00:00 Subject: [ruby-core:80615] [Ruby trunk Feature#13395] Add a method to check for not nil Issue #13395 has been updated by shevegen (Robert A. Heiler). I think that these discussions come up with some frequency; if I recall correctly, Tsuyoshi Sawada was proposing something that I agreed with in principle. But I did point out that the english language appears to have it easier with positive assertions rather than negative ones. I am not against .not_nil? per se, mind you, since it may be symmetrical for .nil? cases and the example give by darix (if nil, if !nil?, unless nil?) but I think it is something that is somewhat showing a limitation of the english language as such. I actually found that the simplest way, for my own code, is to try to formulate things "positively" whenever possible. So: if condition end if condition1 and condition2 end A similar explanation I use for .select or .reject, I almost always go to pick exactly what I need to have. Like a filter where you apply the filtering in a forward fashion (you could of course always revert the filter, like to use .reject rather than .select, or within the block clause, invert the checks). To the suggestion itself in regards to .count, perhaps this may be worthwhile to have some special meaning for what a user may want to count for. In non-legal ruby, consider this: array = [1, 'dog', nil] array.count(! &:nil?) Granted, not very readable. :) Then again, I also consider the & not really readable either. How about: array = [1, 'dog', nil] array.count(:not_nil) or array.count(:not_nil) Hmm... To be honest, I don't think that these examples are really that great. In the above example, using .compact may be simpler: array.compact.size For the latter, perhaps a method that does it, like .not_nil? but I am not sure if this is used that much to warrant an addition. I do somewhat agree with ogarci5 by the way - not necessarily because of the explanation, but because in case 3 he gave, you do not have to use "unless" and neither the invert "operator" "!", which is usually much easier and more straightforward. So in that context, I actually agree, having that flexibility may be a good thing, even if I don't like the name .not_nil? a lot. I still think that it is a limitation of the english language. Consider a backpack in a RPG/rogue-like game. You want to query whether it is empty or filled: if backpack.empty? # Seems simple. if !backpack.empty? # Seems ok although more difficult to understand if backpack.filled? # Hmmm... filled with what? if backpack.not_empty? # Not ideal but perhaps also better than the other two examples before So I kinda semi-agree with ogarci5; I still think that the english language itself is the one that has the biggest problems here with negations, followed by the human brain modeling concepts. (OOP is a modeled concept too after all) ---------------------------------------- Feature #13395: Add a method to check for not nil https://bugs.ruby-lang.org/issues/13395#change-64121 * 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: