Skip to content

Instantly share code, notes, and snippets.

@robbyrussell
Created February 27, 2009 00:21
Show Gist options
  • Save robbyrussell/71219 to your computer and use it in GitHub Desktop.
Save robbyrussell/71219 to your computer and use it in GitHub Desktop.
moving an item in an Array to the front while preserving original order for rest of Array
class Array
def move_to_front_of_line(x)
return [ self[x] ] | self
end
end
>> people = [ 'Alex', 'Carlos', 'Dawn', 'Chris', 'Robby', 'Gary', 'Allison' ]
=> ["Alex", "Carlos", "Dawn", "Chris", "Robby", "Gary", "Allison"]
>>
?> people.move_to_front_of_line(3)
=> ["Chris", "Alex", "Carlos", "Dawn", "Robby", "Gary", "Allison"]
>>
?> people.move_to_front_of_line(people.index('Allison'))
=> ["Allison", "Alex", "Carlos", "Dawn", "Chris", "Robby", "Gary"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment