`Enumerable#detect` {and `#find`} should call `ifnone`; fix #4484

It's an error in ISO specification; 15.3.2.2.4 and 15.3.2.2.7
parent a9fd2e64
......@@ -65,22 +65,20 @@ module Enumerable
end
##
# Call the given block for each element
# which is yield by +each+. Return
# +ifnone+ if no block value was true.
# Otherwise return the first block value
# which had was true.
# Return the first element for which
# value from the block is true. If no
# object matches, calls +ifnone+ and
# returns its result. Otherwise returns
# +nil+.
#
# ISO 15.3.2.2.4
def detect(ifnone=nil, &block)
ret = ifnone
self.each{|*val|
if block.call(*val)
ret = val.__svalue
break
return val.__svalue
end
}
ret
ifnone.call unless ifnone.nil?
end
##
......
......@@ -45,7 +45,7 @@ end
assert('Enumerable#detect', '15.3.2.2.4') do
assert_equal 1, [1,2,3].detect() { true }
assert_equal 'a', [1,2,3].detect("a") { false }
assert_equal 'a', [1,2,3].detect(->{"a"}) { false }
end
assert('Array#each_with_index', '15.3.2.2.5') do
......@@ -64,7 +64,7 @@ end
assert('Enumerable#find', '15.3.2.2.7') do
assert_equal 1, [1,2,3].find() { true }
assert_equal 'a', [1,2,3].find("a") { false }
assert_equal 'a', [1,2,3].find(->{"a"}) { false }
end
assert('Enumerable#find_all', '15.3.2.2.8') do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment