Created
June 18, 2012 21:10
-
-
Save pzaich/2950739 to your computer and use it in GitHub Desktop.
new_inject method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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