From: Xavier Noria Date: 2007-08-03T22:39:55+09:00 Subject: Re: Determining the common prefix for several strings El Aug 3, 2007, a las 1:35 PM, Xavier Noria escribi�: > prefix = '' > min, max = items.sort.values_at(0, -1) > min.split(//).each_with_index do |c, i| > break if c != max[i, 1] > prefix << c > end > puts prefix Yet another iteration: the prefix can be extracted with a regexp, it's shorter although perhaps more obscure: min, max = items.sort.values_at(0, -1) puts min+max =~ /(.).{#{min.length-1}}(?!\1)/m ? $` : min I don't like the ternary and the fact that we look for the first mismatch instead of extracting directly the prefix in one shot. -- fxn