Skip to content

Instantly share code, notes, and snippets.

@IceDragon200
Created September 15, 2013 17:11
Show Gist options
  • Save IceDragon200/6572605 to your computer and use it in GitHub Desktop.
Save IceDragon200/6572605 to your computer and use it in GitHub Desktop.
An implementation of each_with_objects and map_with_objects for ruby
module Enumerable
def each_with_objects(*args, &block)
return to_enum(:each_with_objects, *args) unless block_given?
each do |obj|
yield obj, *args
end
end unless method_defined?(:each_with_objects)
def map_with_objects(*args, &block)
return to_enum(:map_with_objects, *args) unless block_given?
map do |obj|
yield obj, *args
end
end unless method_defined?(:map_with_objects)
end
p (1...10).each_with_objects(2, 4).to_a
p Array.new(14) { [] }.map_with_objects(2, 4, &:push)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment