On Sat, 27 May 2006, Kevin Olbrich wrote: > Here's a simple problem that seems like a good Ruby exercise. > > Given a string containing an Excel-like range specifier like this: > "C5:E8", generate an array of all the cells in the array. > > In this case, the output should be something like this.... > > ["C5", "C6", "C7", "C8", "D5", "D6", "D7", "D8", "E5", "E6", "E7", "E8"] > > Ideally, this should also work for a partial range like "A:C" => ["A", > "B", "C"] and "12:15" => ["12","13","14","15"] > > Extra points for style and bonus kudos for a one-liner. i didn't feel like golfing before coffee, but here's one: harp:~ > cat a.rb class ExcelRange < ::Array def initialize spec a, b = spec.to_str.split %r/:/ alpha_a, numeric_a = a.split %r/(\d+)/ alpha_b, numeric_b = b.split %r/(\d+)/ a = alpha_a ? (alpha_a .. alpha_b).to_a : [] b = numeric_a ? (numeric_a .. numeric_b).to_a : [] case [a.empty?, b.empty?] when [true, true] # nil when [true, false] replace b when [false, true] replace a when [false, false] a.each{|alpha| push(*b.map{|numeric| "%s%d" % [alpha, numeric]})} end end end def ExcelRange(*a, &b) ExcelRange.new(*a, &b) end p ExcelRange("C5:E8") p ExcelRange("A:C") p ExcelRange("12:15") harp:~ > ruby a.rb ["C5", "C6", "C7", "C8", "D5", "D6", "D7", "D8", "E5", "E6", "E7", "E8"] ["A", "B", "C"] ["12", "13", "14", "15"] regards. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama