Skip to content

Instantly share code, notes, and snippets.

@toddsiegel
Created December 9, 2012 08:01
Show Gist options
  • Save toddsiegel/4243843 to your computer and use it in GitHub Desktop.
Save toddsiegel/4243843 to your computer and use it in GitHub Desktop.
Array#move: Move the first matched element in an array. Safely wraps and supports negative distances
class Array
def move(element, distance)
current_pos = index(element)
return self if current_pos.nil?
new_pos = (current_pos+distance) - ((current_pos+distance)/length)*length
delete(element)
insert(new_pos, element)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment