From: Brian Candler Date: 2007-08-06T19:01:03+09:00 Subject: Re: Determining the common prefix for several strings Here's a simple solution from my simple brain. It just takes the common prefix between the first two items, then the common prefix between this and the third, etc. arr = %w{ item001 item004 item002 item002b item002C item_5 item_10 itemize_this } prefix = arr.first.dup arr[1..-1].each do |e| prefix.slice!(e.size..-1) if e.size < prefix.size # optimisation prefix.chop! while e.index(prefix) != 0 end puts prefix