Skip to content

Instantly share code, notes, and snippets.

@arya
Created July 15, 2009 18:21
Show Gist options
  • Save arya/147878 to your computer and use it in GitHub Desktop.
Save arya/147878 to your computer and use it in GitHub Desktop.
# inject but doesn't need you to return the memo, uses the same object. Doesn't work with immutable (i.e. summing)
module Enumerable
def inject_into(obj)
each {|e| yield(obj, e)}
obj
end
end
a = [1,2,3].inject_into([]) do |m, e|
m << (e + 1)
end
puts a.inspect # => [2,3,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment