Skip to content

Instantly share code, notes, and snippets.

@pzaich
Created June 18, 2012 21:10
Show Gist options
  • Select an option

  • Save pzaich/2950739 to your computer and use it in GitHub Desktop.

Select an option

Save pzaich/2950739 to your computer and use it in GitHub Desktop.
new_inject method
module Enumerable
def new_inject(*initial_value)
loaded_array = []
#change to Ranges to Array
if self.class == Range
loaded_array = self.to_a
else
loaded_array = self
end
#Load an initial value passed in into the array
if initial_value != []
initial_value = initial_value.first
else
initial_value = loaded_array.first
loaded_array = loaded_array[1..loaded_array.length-1]
end
#increment initial_value
loaded_array.each_with_index do |value, index|
initial_value = yield(initial_value, value)
end
initial_value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment