From: Martin DeMello Date: 2008-03-26T06:15:05+09:00 Subject: Re: golf: tabs to bullets On Tue, Mar 25, 2008 at 2:02 PM, Martin DeMello wrote: > Given: > an outline list, with each line indented by a series of tabs > a list of bullets (assume a circular list, for simplicity) > > Write a function that replaces every tab with two spaces, except for > the last one, which is replaced by a bullet. Code is worth a thousand descriptions: #!/usr/bin/ruby file = ARGV[0] MARKERS = "*-ox".split(//) def bullet(n) MARKERS[n % MARKERS.length] end IO.foreach(file) {|line| a = line.gsub(/^\t+/, "") n = line.length - a.length - 1 replace = n == -1 ? "" : " "*(n)+ bullet(n) + " " out = line.gsub(/^\t+/, replace) puts out } martin