summaryrefslogtreecommitdiffhomepage
path: root/mrblib/enum.rb
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-07-13 18:05:01 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2019-07-13 18:05:01 +0900
commitb4cdced22fe1f25d8cf6fc499a2e2c9875f17e9e (patch)
tree44697631bb10ae64c9b35cd83beac582f18d4fca /mrblib/enum.rb
parenta9fd2e646492699894d33eaf3de7336356ce6726 (diff)
downloadmruby-b4cdced22fe1f25d8cf6fc499a2e2c9875f17e9e.tar.gz
mruby-b4cdced22fe1f25d8cf6fc499a2e2c9875f17e9e.zip
`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
Diffstat (limited to 'mrblib/enum.rb')
-rw-r--r--mrblib/enum.rb16
1 files changed, 7 insertions, 9 deletions
diff --git a/mrblib/enum.rb b/mrblib/enum.rb
index 9bd74e1c4..fe40b4d27 100644
--- a/mrblib/enum.rb
+++ b/mrblib/enum.rb
@@ -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
##