Xavier Noria
4/27/2007 12:32:00 AM
On Apr 27, 2007, at 12:55 AM, Dan Stevens (IAmAI) wrote:
> Something in caught me out today. I wanted to test that the contents
> of the array where all instances of particular class, and if not, do
> stuff. The example isn't exactly what my code did, but it illustrates
> my point:
>
> if not my_arr.all? { |i| i.is_a(Integer) }
> # Do stuff
> end
>
> I expecting it to 'do stuff', however I found it was not. I discovered
> that 'my_arr' was empty, which was not a surprise, but what did
> surprise me was that all? called on an empty array appears to always
> return true.
>
> I don't know about you, but these seems counter-intuitive to me. Does
> anyone agree or disagree?
Yeah, it is suprising at first sight but that's standard semantics in
math.
A for all statement like that holds always for the empty set because
otherwise you'd need to show an element of the empty set (impossible)
for which the test is false. The reason is
not for all x in S P(x)
is logically equivalent to
there exists x in S such that not P(x)
This is a corner case, note that it does not depend on P.
-- fxn