From: Logan Capaldo Date: 2005-12-22T05:33:48+09:00 Subject: Re: move to front of array On Dec 21, 2005, at 3:08 PM, Payton Swick wrote: > Hi, > > 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 } > > Better/cleaner/shorter ways to do it? > > -Payton > I don't know about shorter but... require 'enumerator' array = %w(a B c d Cool e f G) index = array.to_enum(:each_index).find { |i| array[i] =~ /cool/i } array.unshift( array.delete_at( index ) )