Moving Elements in Ruby Arrays
By snuxoll - October 9th, 2008
Tagged:
Today in my coding of droppit I needed to keep an ordered list of mimetypes sorted by priority, as such I needed some way to 'move' them around the array. Since there is no included way in the ruby Array class to do this, with the help of my friends in #ruby on freenode I have this snippet of code for you.
# Mixin for Array that allows you # to move elements arround easily module Reorderable def move(from, to) insert(to, delete_at(from)) end end
Here's some example usage
priorities = ['text/uri-list', 'x-mozilla/link'] priorities.extend(Reoderable) priorities.move(1, 0) priorities => ['x-mozilla/link', 'text/uri-list']














Comments
Post new comment