Skip to content

Instantly share code, notes, and snippets.

@hilem
Created August 2, 2012 14:23
Show Gist options
  • Save hilem/3237438 to your computer and use it in GitHub Desktop.
Save hilem/3237438 to your computer and use it in GitHub Desktop.
# 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