Skip to content

Instantly share code, notes, and snippets.

@eregon eregon/zip.rb
Created Apr 5, 2011

Embed
What would you like to do?
a Ruby implementation of the changes I explain in http://redmine.ruby-lang.org/issues/4539
# 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
You can’t perform that action at this time.