robbyrussell (owner)

Forks

Revisions

gist: 71219 Download_button fork
public
Description:
moving an item in an Array to the front while preserving original order for rest of Array
Public Clone URL: git://gist.github.com/71219.git
Embed All Files: show embed
move-to-front-of-line.rb #
1
2
3
4
5
class Array
  def move_to_front_of_line(x)
    return [ self[x] ] | self
  end
end
usage.rb #
1
2
3
4
5
6
7
8
>> 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"]