From: Florian Frank Date: 2005-12-22T05:29:25+09:00 Subject: Re: move to front of array Payton Swick wrote: > I have an array of Strings, and I'd like to find one of the items by > Regexp, then move that item to the front of the array, eg: > > array = %w(a B c d Cool e f G) > array.unshift(array.slice!(array.index(array.find { |i| i =~ /cool/i > }))) if array.find { |i| i =~ /cool/i } In 1.8.x: require 'enumerator' if f = array.enum_for(:each_index).find { |i| array[i] =~ /cool/i } array.unshift array.delete_at(f) end In 1.9.x you can do: array.unshift array.delete_at(array.index { |x| x =~ /cool/i }) -- Florian Frank