From: Rick DeNatale Date: 2010-07-31T00:49:59+09:00 Subject: Re: [].all?{} and [].any?{} Behavior On Thu, Jul 29, 2010 at 5:27 PM, John Sikora wrote: > The second interesting thing is that a result of this behavior is that > for the same check, all? will give a result of true, while any? will > give a result of false. This seems contradictory. Well, there's a theoretical basis for this. Enumeration#all? is an implementation of the universal quantifier from predicate logic (that upside down A) symbol. By convention the universal quantifier evaluates to true for an empty set: http://en.wikipedia.org/wiki/Universal_quantification#The_empty_set http://en.wikipedia.org/wiki/Vacuous_truth > > I would prefer that for [], both all? and any? would give a result of > false for any check. So I have over-ridden Array#all?, returning false > if self == []. My main motivation for doing so is in situautions such > as: > > obj_array.find_all{|obj| obj.attr_1 == x}.all?{|obj| obj.attr_2 == y} > > If the find_all returns [], I want the all? result to be false, not > true. > > I assume that others have run across this but some quick searches did > not turn up anything. I am wondering how others deal with this such as > over-riding as I do, checking for [] each time (which does not seem very > Ruby-like), or even leaving the operation as is because for some, it may > be the desired behavior. > > Finally, are there any potential detrimental effects that might occur > due to the behavior modification that I made. I am not a Rails user (if > that matters), I mainly use Ruby for scripting and hardware control > applications (and I am interested in learning as much as I can about > Ruby because I like it so much). For your own usage as long as it doesn't mess up some other code you are using, feel free. For library code, such as in a gem I think it would be better to think up some other method name rather than changing the standard, e.g. module Enumerable def non_vacuous_all?(&b) !empty? && all?(&b) end end [3].all? {|element| element == 3 } # => true [3].all? {|element| element != 3 } # => false [].all? {|element| element == 3 } # => true [].all? {|element| element != 3 } # => true [3].non_vacuous_all? {|element| element == 3 } # => true [3].non_vacuous_all? {|element| element != 3 } # => false [].non_vacuous_all? {|element| element == 3 } # => false [].non_vacuous_all? {|element| element != 3 } # => false [].any? {|element| element == 3 } # => false [].any? {|element| element != 3 } # => false There may be a better name than non_vacuous_all? but I can't think of one. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale