提交 f061ffd9 编写于 作者: M Marc-Andre Lafortune

Trivial optimization for Enumerable#each_with_object

上级 816abecf
...@@ -76,10 +76,10 @@ def sum(identity = 0, &block) ...@@ -76,10 +76,10 @@ def sum(identity = 0, &block)
# #
# (1..5).each_with_object(1) { |value, memo| memo *= value } # => 1 # (1..5).each_with_object(1) { |value, memo| memo *= value } # => 1
# #
def each_with_object(memo, &block) def each_with_object(memo)
return to_enum :each_with_object, memo unless block_given? return to_enum :each_with_object, memo unless block_given?
each do |element| each do |element|
block.call(element, memo) yield element, memo
end end
memo memo
end unless [].respond_to?(:each_with_object) end unless [].respond_to?(:each_with_object)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册