Project

General

Profile

Bug #6760

unexpected behavior in Enumerable method all? if collection is empty

Added by Anton Shemerey over 3 years ago. Updated over 3 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
ruby -v:
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
Backport:
2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
[ruby-core:46566]

Description

if collection is empty all? always return true even if we call it with undefined method

for example

-> [].all?(&:undefined_mathod) #=> true
-> {}.all?(&:undefined_mathod) #=> true

etc.

my fix for it un ruby:

module Enumerable
alias_method :original_methodall?, :all?

def all?(&block)
return false if count == 0

if block_given?
  _original_method_all?(&block)
else
  _original_method_all?
end

end
end

example test file in attach

test_for_enumerable_all.rb Magnifier (3.21 KB) Anton Shemerey, 07/21/2012 01:11 AM

History

#1 [ruby-core:46571] Updated by Marc-Andre Lafortune over 3 years ago

  • Status changed from Open to Rejected

This is not a bug. This behavior is clear from the documentation, both in the form with and without a block:

"The method returns true if the block never returns false or nil."

"If the block is not given [...] will return true only if none of the collection members are false or nil."

You may open a feature request if you want, but I feel it won't be accepted, because the current behavior makes sense (see http://en.wikipedia.org/wiki/Vacuous_truth ). It would be an incompatible change and would need very good justification.

#2 [ruby-core:46572] Updated by Anton Shemerey over 3 years ago

thx for your explanation now it's clear.
I think this bug can be closed

bunch of thanks, i don't thought about this problem from "Vacuous truth" side %-)

Also available in: Atom PDF