Skip to content

Instantly share code, notes, and snippets.

/inject.rb Secret

Created May 17, 2016 20:45
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 anonymous/9159f68b15edd21aa2729bf98ede7f73 to your computer and use it in GitHub Desktop.
Save anonymous/9159f68b15edd21aa2729bf98ede7f73 to your computer and use it in GitHub Desktop.
class Array
def my_inject(result)
self.each { |current_num| result = yield(result, current_num) }
result
end
end
puts [5, 4, 2].my_inject(2) { |sum, current_num| sum + current_num }
# 13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment