Created
August 2, 2012 14:23
-
-
Save hilem/3237438 to your computer and use it in GitHub Desktop.
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
# http://blog.jazzychad.net/2012/08/01/array-iteration-problem.html | |
def argh(arr, val, n) | |
# change to arr2 = arr if you want to change the array in place | |
arr2 = arr.dup | |
tmp_range = (0..arr2.length) | |
tmp_range = n < 0 ? (n..arr2.length) : (0..(n-1)) unless n == 0 | |
tmp = [] | |
arr2.each_with_index { |v, i| tmp << [v, i] if v == val } | |
tmp[tmp_range].map! { |v| v[0] += val } | |
tmp.each { |v| arr2[v[1]] = v[0] } | |
arr2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment