Created
February 27, 2009 00:21
-
-
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
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
class Array | |
def move_to_front_of_line(x) | |
return [ self[x] ] | self | |
end | |
end |
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
>> 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