Enumerable#all? and empty arrays

A

ara.t.howard

Hello

I found this to be somewhat amusing:
a = [] => []
a.all? => true
a.all? { |i| i == 10 }
=> true

Is there any reasoning for Enumerable#all? to always return true for
empty arrays?

Best regards,
Andre

well - it's either that or false. we'd need maybe for it to really make
sense.

consider

[].all?{|x| x.nil?} # true or false??

either is arbitrary

-a
 
F

Florian Frank

Andre said:
=> []
=> true
=> true

Is there any reasoning for Enumerable#all? to always return true for
empty arrays?
I think they are modeled after universal and existential quantifiers in
predicate calculus:

a.all?(&b) is true iff for all x in a b[x].

This leads to a.all? being true.

a.any?(&b) is true iff there exists an x in a for which b[x].

This leads to a.any? being false.
 
M

Mauricio Fernandez

Hello

I found this to be somewhat amusing:
a = [] => []
a.all? => true
a.all? { |i| i == 10 }
=> true

Is there any reasoning for Enumerable#all? to always return true for
empty arrays?

a.all?{ ... } == true

can be read as "All elements in a satisfy { ... }", which we can interpret as
"there is no element in a such that { ... } is not satisfied", which is
obviously true if a is empty.
 
A

Andre Nathan

can be read as "All elements in a satisfy { ... }", which we can interpret as
"there is no element in a such that { ... } is not satisfied", which is
obviously true if a is empty.

OK, thank you all for the explanations :)

Andre
 
J

Just Another Victim of the Ambient Morality

Andre Nathan said:
Hello

I found this to be somewhat amusing:
a = [] => []
a.all? => true
a.all? { |i| i == 10 }
=> true

Is there any reasoning for Enumerable#all? to always return true for
empty arrays?

It's mathematically consistent.

You may be surprised to hear this but mathematicians are generally
optimists. Thus, if a condition is "vacuously satisfied," then we generally
say the condition is true...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top