Anybody know what’s going on here?
[].all?
=> true
After 5 years of using ruby, it still
surprises me at times–shouldn’t that require a block?
-roger-
Anybody know what’s going on here?
[].all?
=> true
After 5 years of using ruby, it still
surprises me at times–shouldn’t that require a block?
-roger-
Roger P. wrote in post #1005933:
Anybody know what’s going on here?
[].all?
=> trueAfter 5 years of using ruby, it still
surprises me at times–shouldn’t that require a block?
-roger-
None of your collection members are false or nil, so all? returns true.
7stud – wrote in post #1006058:
Roger P. wrote in post #1005933:
Anybody know what’s going on here?
[].all?
=> trueAfter 5 years of using ruby, it still
surprises me at times–shouldn’t that require a block?
-roger-class Enumerable
enum.all? [{|obj| block } ] → true or false
…
…
If the block is not given, Ruby adds an implicit block of {|obj| obj}
(that is all? will return true only if none of the collection members
are false or nil.)None of your collection members are false or nil, so all? returns true.
I think the result is counterintuitive.
[].none? also returns true.
Joey Z. wrote in post #1006108:
Anybody know what’s going on here?
[].all?
=> trueNone of your collection members are false or nil, so all? returns true.
I think the result is counterintuitive.
Let’s check Haskell:
any (==4) [3, 4]
=> True
any (==4) []
=> False
all (==4) [3, 4]
=> False
all (==4) []
=> True
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs