Skip to content

Instantly share code, notes, and snippets.

@eregon
Created April 5, 2011 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eregon/903388 to your computer and use it in GitHub Desktop.
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 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