Created
April 5, 2011 10:23
-
-
Save eregon/903388 to your computer and use it in GitHub Desktop.
a Ruby implementation of the changes I explain in http://redmine.ruby-lang.org/issues/4539
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is not efficient, it just shows the logic | |
class Array | |
alias _zip zip | |
def zip(*enums) | |
if block_given? | |
_zip(*enums).map(&Proc.new) | |
elsif Symbol === enums.last | |
sym = enums.pop | |
_zip(*enums).map { |elements| elements.reduce(sym) } | |
else | |
_zip(*enums) | |
end | |
end | |
end | |
p [1,2,3].zip([6,5,4]) { |a,b| a+b } # => [7, 7, 7] | |
p [1,2,3].zip([6,5,4], :+) # => [7, 7, 7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment