# Ruby gotcha? \[\].all? always returns true

**URL:** <https://www.ruby-forum.com/t/ruby-gotcha-all-always-returns-true/93054>\
**Category:** Ruby\
**Created:** [April 27, 2007, 12:55am UTC](https://www.ruby-forum.com/t/ruby-gotcha-all-always-returns-true/93054 "2007-04-27T00:55:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 27, 2007, 12:55am UTC](https://www.ruby-forum.com/t/ruby-gotcha-all-always-returns-true/93054/1 "2007-04-27T00:55:47Z")

</div>

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?

I’ve now resorted to any? instead, like as follows:

if not my\_arr.any? { |i| not i.is\_a(Integer) }

# Do stuff

end

As far as it seems [].any? always returns false. The code should also  
be slightly more efficient as it doesn’t always have to check every  
item of the array, not that I expect it to get very big.

---

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 27, 2007, 2:17am UTC](https://www.ruby-forum.com/t/ruby-gotcha-all-always-returns-true/93054/2 "2007-04-27T02:17:52Z")

</div>

On 4/26/07, Dan Stevens (IAmAI) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

---

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 27, 2007, 2:32am UTC](https://www.ruby-forum.com/t/ruby-gotcha-all-always-returns-true/93054/3 "2007-04-27T02:32:53Z")

</div>

On Apr 27, 2007, at 12:55 AM, Dan Stevens (IAmAI) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 27, 2007, 2:21pm UTC](https://www.ruby-forum.com/t/ruby-gotcha-all-always-returns-true/93054/4 "2007-04-27T14:21:33Z")

</div>

“Dan Stevens (IAmAI)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> anyone agree or disagree?  
> It seems perfectly intuitive to me.

There’s a local law that states that all dogs must wear leashes when  
not confined behind a fence. We are in full compliance with that law,  
even though our front yard has no fence and we own no leashes - this  
is because we have no dogs.

> I’ve now resorted to any? instead, like as follows:
> 
> if not my\_arr.any? { |i| not i.is\_a(Integer) }
> 
> # Do stuff
> 
> end
> 
> As far as it seems [].any? always returns false. The code should also  
> be slightly more efficient as it doesn’t always have to check every  
> item of the array, not that I expect it to get very big.

Actually, all? has the same efficiency characteristics. That is, the  
instant it finds something false, it returns false.

---

<div class="post-metadata">

**Author:** ![Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ad7895/32.png) [@Dan\_Stevens\_SIAmAI](https://www.ruby-forum.com/u/Dan_Stevens_SIAmAI)\
**Post date:** [April 27, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/ruby-gotcha-all-always-returns-true/93054/5 "2007-04-27T20:39:49Z")

</div>

On Apr 27, 2007, at 2:21 PM, Daniel M. wrote:

> > return true.
> > 
> > I don’t know about you, but these seems counter-intuitive to me. Does  
> > anyone agree or disagree?
> 
> It seems perfectly intuitive to me.
> 
> There’s a local law that states that all dogs must wear leashes when  
> not confined behind a fence. We are in full compliance with that law,  
> even though our front yard has no fence and we own no leashes - this  
> is because we have no dogs.

A good explanation.

Let me elaborate it a bit more. The parallelism there goes: "you are  
within the law if all your dogs satisfy ". Can I say that  
"all my dogs satisfy " is true if I have no dog? Why? In  
what sense can I say something holds in an empty collection?

To see why the response is yes, I think we need to go a little step  
further: The point is that a policeman should prove one of your dogs  
does not satisfy to say you’ve break the law. It’s  
because that’s impossible that you are within the law as far as that  
law concerns. Therefore, since you are either within or outside the  
law, "all your dogs satisfy " necessarily holds.

That’s the formal equivalence I mentioned earlier (what makes  
logicians define for all and there exists that way is our intuitive  
reasoning about dogs and laws, formal logic as such does not justify  
per se that’s OK that way.)

– fxn
